Write a program to show copy constructor and destructor call.
#include<iostream> using namespace std; class A { private: int x,y; public: A() { cout << "default constructor" << endl; } A(int a,int b):x(a),y(b) { cout << "parameterized constructor" << endl; } ~A() { cout << "destructor" << endl; } A(A& ob):x(ob.x),y(ob.y) { cout << "copy constructor" << endl; } void print() { cout << "x = " << x << " y = " << y << endl; } }; int main() { A obj1,obj2(1,4); A obj3 = obj2;//A obj3(obj2) obj1.print(); obj2.print(); obj3.print(); return 0; }
Output
default constructor parameterized constructor copy constructor x = -2 y = 1998018666 x = 1 y = 4 x = 1 y = 4 destructor destructor destructor