Copy Constructor Iet
Copy Constructor Iet
Manoj Pawaiya
Lect.(IT)
IET davv, Indore
mob- 9926839606
Copy Constructor
• 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
• Item i2(i1);
• Item i2=i1;
• i2,=i1;
To calculate factorial of a given number using copy
constructor.
double calculate()
class copy {
{ fact=1;
int var,fact; for(int i=1;i<=var;i++)
public: {
copy(int temp) fact = fact * i;
{ }
var = temp; returnfact;
}
}
};
void main()
{ Output:
Enter the Number: 5
clrscr();
Factorial is: 120
int n; Factorial is: 120
cout<<"\n\tEnter the Number : ";
cin>>n;
copy obj(n);
copy cpy=obj;
cout<<"\n\t"<<n<<" Factorial is:"<<obj.calculate();
cout<<"\n\t"<<n<<" Factorial is:"<<cpy.calculate();
getch();
}