A copy constructor is a special type of constructor by using which we can initialize the data members of a newly created object by existing objects. The compiler provides a default copy constructor.
Syntax of calling a copy constructor is:
complex c1 (c2);
where complex is name of class, C1 is new object and C2 is existing object.
Program:
#include<iostream.h>
#include<conio.h>
class complex
{
int r, i;
public: complex(int a, int b)
void show();
}
complex : : complex(int a, int b)
{r=a ; i=b;}
void complex : : show()
{cout<<r<<"+i"<<i<<endl;}
void main()
{
int x,y;
clrscr();
cout<<"Enter real part:"; cin>>x;
cout<<"Enter imag part:": cin>>y;
complex C1 (C2);
cout<<"C2=";
C1.show();
getch();
}
0 Comment(s)