Program for Copy Constructor Using C++ Programming

-

Affiliate Disclosure: Every purchase made through our affiliate links earns us a pro-rated commission without any additional cost to you. Here are more details about our affiliate disclosure.

Program to calculate factorial of a given number using copy constructor in C++

Algorithm

Step 1: Start the program.
Step 2: Declare the class name as Copy with data members and member functions.
Step 3: The constructor Copy() with argument to assign the value.
Step 4: To cal the function calculate() do the following steps.
Step 5: For i=1 to var do
Step 6: Calculate fact*i to assign to fact.
Step 7: Increment the value as 1.
Step 8: Return the value fact.
Step 9: Print the result.
Step 10: Stop the program.

Program

/* Program to calculate factorial of a given number using copy constructor in C++
Pardeep Patel @ studywarehouse.com */

#include<iostream.h>
#include<conio.h>
class copy
{
int var,fact;
public:

copy(int temp)
{
var = temp;
}

double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int n;
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();
}

Output

Enter the Number: 5
Factorial is: 120
Factorial is: 120

Related Articles

Like our Article/ Blog? Can buy a Buttermilk for our team.. Click here

Pardeep Patelhttps://pardeeppatel.com/
Hi!, I am Pardeep Patel, an Indian passport holder, Traveler, Blogger, Story Writer. I completed my M-Tech (Computer Science) in 2016. I love to travel, eat different foods from various cuisines, experience different cultures, make new friends and meet other.

Share this article

-- Advertisement --

LEAVE A REPLY

Please enter your comment!
Please enter your name here

-- Advertisement --