C++ | Constructors | Question 5

Output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


(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


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.