Copy Constructor in C++ example

Copy Constructor in C++ example

/********************************************************************************************************
*************Designed and Developed by V2Geeks******************************************
********************************************************************************************************/

 // Example of Copy constructor.
 #include<iostream.h>
 #include<conio.h>
 class Test
 {
 public:
 int id;
Test()             // default constructor
 {

 }
Test(int x ) //to initialize class member id 
 {
        id=x;
}
Test( Test &R)// This is copy constructor
 {
           id=R.id;
 }
 void display()
 {
        cout<<id<<endl;
 }
 };
 void main()
 {
         Test A(100);
         A.display();      //it will print 100
         Test B(A);       //copy constructor called
         B.display();   //it will print 100
         Test C=A;  //copy constructor called again
         C.display();   //it will print 100
        Test D;
        D=A; //This is not copy constructor but it is copy initialization.
        D.display(); //it will also  print 100
 }
Reactions: