Wednesday, May 6, 2009

Copy Constructor

Copy Constructor

Passing object as argument to another constructor is called copy constructor.

header files

class copy
{
int a,b,c;
public:
copy(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
copy(copy & ob) // copy constructor declaration
{
a=ob.a;
b=ob.b;
c=ob.c;
}
void display()
{
cout << a << endl << c << endl;
}
};

void main()
{
copy ob1(5,10,20);
copy pb2(ob1); // object as an argument
clrscr();
ob1.display();
ob2.display();
getch();
}

output will be
5
10
20
5
10
20

No comments:

Post a Comment