0% found this document useful (0 votes)
2 views4 pages

Copy Constructor Iet

The document explains the concept of a copy constructor, which is a special constructor that creates a new object as a copy of an existing one. It provides examples of how to use a copy constructor in C++ to calculate the factorial of a number. The code demonstrates the implementation of the copy constructor and the calculation of factorial for a given input.

Uploaded by

aaammm1522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Copy Constructor Iet

The document explains the concept of a copy constructor, which is a special constructor that creates a new object as a copy of an existing one. It provides examples of how to use a copy constructor in C++ to calculate the factorial of a number. The code demonstrates the implementation of the copy constructor and the calculation of factorial for a given input.

Uploaded by

aaammm1522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Copy-constructor

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();
}

You might also like