Write a C++ program which perform the following:
1. Create a Class factorial
2. Create Private Member Variable and initialize it with Member
initialization list and with value zero
3. Now, take inputs from user
4. Create a Public Member Function Calculate( ) outside the class,
to calculate the factorial of a given input
5. Create another Public Member Function Result( ) outside the
class, to print the results on screen
6. Now, create a constant object of the class which perform following:
a. Passes the values to the Calculate( ) function
b. Call the Result( ) function to show result
#include<iostream>
using namespace std;
class Factorial
{
int n;
public:
Factorial(int i=0):n(i){}
int access()const
{
return n;
}
unsigned long int Calculate()const;
void display()const;
};
unsigned long int Factorial::Calculate()const
{
unsigned long f=1;
int i=0,j;
j=access();
for(i=1;i<=j;i++)
{
f=f*i;
}
return f;
}
void Factorial::display()const
{
cout<<"\nFactorial of "<<access()<<" is equal to "<<Calculate();
}
int main()
{
const Factorial f1;
int* ptr=(int*)&f1;
int n1;
cout<<"Enter the number whose factorial is to be calculated ";
cin>>n1;
*ptr=n1;
f1.display();
}
Comments
Leave a comment