C++ Program to find factorial of a number using copy constructor
Program
#include<iostream>
using namespace std;
class FactorialCopy {
public:
int fact, num, i;
FactorialCopy(int n) {
num = n;
fact = 1;
}
FactorialCopy(FactorialCopy &n) {
num = n.num;
fact = 1;
}
void calculate() {
for (int i = 1; i <= num; i++)
fact = fact * i;
}
void display() { cout << "Factorial of " << num << " is " << fact << endl; }
};
int main() {
int num;
cout << "Enter the number to find factorial:\t";
cin >> num;
cout << endl;
cout << "Printing from Constructor:" << endl;
FactorialCopy fact(num);
fact.calculate();
fact.display();
cout << endl;
cout << "Printing from Copy Constructor:" << endl;
FactorialCopy factCopy(fact);
factCopy.calculate();
factCopy.display();
return 0;
}
Output
Enter the number to find factorial: 6
Printing from Constructor:
Factorial of 6 is 720
Printing from Copy Constructor:
Factorial of 6 is 720