Thursday, 19 January 2012

Program Of Copy Constructor In C++

#include<iostream.h>
#include<conio.h>
class add
{
private:
int x,y;           // Data Member
public:

add(int p,int q)   // Constructor
{
x=p;
y=q;
}
void display()
{
int r;
r=x+y;
cout<<"\n NAswer="<<r;
}


};
main()
{
clrscr();
int a,b,c;
cout<<"Enter First NUmber=";
cin>>a;
cout<<"Enter Second NUmber=";
cin>>b;
add a1(a,b);
add a2(a1);  //Copy COnstructor
a1.display();
a2.display();
getch();
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------


The copy constructor is a special kind of constructor which creates a new object which is a copy of an existing one, and does it efficiently.The copy constructor receives an object of its own class as an argument, and allows to create a new object which is copy of another without building it...


No comments:

Post a Comment