Program demonstrating use of copy constructor in C++.

using namespace std;
class code
int id;
code(int a){id=a;}
code(code &x) // copy constructor.
{; //copy in the value.
void display(void)
cout << id;
int main()
code A(100); // object A is created & initialized.
code B(A); //copy constructor called.
code C = A; // copy constructor called again.
code D;
D = A; // copy constructor not called.
cout << “n id of A:”; A.display();
cout << “n id of B:”; B.display();
cout << “n id of C:”; C.display();
cout << “n id of D:”; D.display();
return 0;
Output :
id of A:100
id of B:100
id of C:100
id of D:100

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.