Monday, March 22, 2010

Example of copy constructor in c++

#include
class complex
{
int id;
public:
complex() { } // default constructor
complex(int a)
{
id=a;
}
complex(complex &x) // copy constructor
{
id=x.id;
}
void display (void) // definintion of display()
{
cout<<<"\n";
}
}; // end of class

void main()
{
complex a(100);
complex b(a);
complex c=a; // copy constructor invoked
complex d=a;
cout << "the value of a is " ;
a.display();
cout <<" the value of b is " ; b.display();
cout << " the value of c is ";c.display();
cout << " the value of d is ";d.display();
}// end of main