Sunday, January 1, 2012

Copy constructor in 'C++'

Example



#include<iostream.h>
#include<conio.h>
class demo1
{
int d,e;
public:
demo1(int a,int b)
{
d=a;
e=b;
}
demo1(demo1 &x)
{
d=x.d;
      e=x.e;
}
void add()
{
cout<<d+e;
}
};
int main()
{
clrscr();
demo1 d(4,5),d3(2,3);
demo1 d1(d);
demo1 d2=d3;


       cout<<"\nadd of d:";d.add();
       cout<<"\nadd of d1:";d1.add();
cout<<"\nadd of d2:";d2.add();
cout<<"\nadd of d3:";d3.add();
getch();
return 0;
}
/*=============================================================
output


add of d:9
add of d1:9
add of d2:5
add of d3:5
*/

No comments:

Post a Comment