pgrm_017.cpp (414B)
1 /* Copy Constructor */ 2 3 #include<iostream> 4 using namespace std; 5 class A { 6 private: 7 int x; 8 double y; 9 public: 10 A(int a, double b) { 11 x = a; y = b; 12 } 13 A(A & ob) { 14 // Copy constructor 15 x = ob.x; 16 y = ob.y; 17 } 18 void display() { 19 cout << "x = " << x << " y = " << y << endl; 20 } 21 }; 22 23 int main() { 24 A obj(20, 23.5); 25 cout << "obj: "; 26 obj.display(); 27 A obj2(obj); 28 cout << "obj2: "; 29 obj2.display(); 30 return 0; 31 }