Skip to content
Related Articles

Related Articles

Improve Article

C++ | Constructors | Question 5

  • Last Updated : 28 Jun, 2021

Output of following program?

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.




#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

My Personal Notes arrow_drop_up
Recommended Articles
Page :