C++ | Constructors | Question 5

Output of following program?

#include<iostream>
using namespace std;

class Point {
public:
    Point() { cout << "Normal Constructor called\n"; }
    Point(const Point &t) { cout << "Copy constructor called\n"; }
};

int main()
{
   Point *t1, *t2;
   t1 = new Point();
   t2 = new Point(*t1);
   Point t3 = *t1;
   Point t4;
   t4 = t3;
   return 0;
}

(A) Normal Constructor called
Normal Constructor called

Normal Constructor called

Copy Constructor called

Copy Constructor called

Normal Constructor called

Copy Constructor called

(B) Normal Constructor called
Copy Constructor called

Copy Constructor called

Normal Constructor called

Copy Constructor called

(C) Normal Constructor called
Copy Constructor called

Copy Constructor called

Normal Constructor called



Answer: (C)

Explanation: See following comments for explanation:

Point *t1, *t2;   // No constructor call
t1 = new Point(10, 15);  // Normal constructor call
t2 = new Point(*t1);   // Copy constructor call 
Point t3 = *t1;  // Copy Constructor call
Point t4;   // Normal Constructor call
t4 = t3;   // Assignment operator call 


Quiz of this Question






Practice Tags :

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.