After getting a lot of help from the internet i finally was able to write the program below, but i have some confusions now. CONFUSION 1: Can anyone explain to me the function of access method.
int access()const
{
return n;
}
CONFUSION 2 : I don't understand the working of the lines below in he program i have written.
int* ptr=(int*)&f1;
Here is the program:
#include<iostream>
using namespace std;
class Factorial
{
int n;
public:
Factorial():n(0){}
int access()const
{
return n;
}
int Calculate()const;
void display()const;
};
int Factorial::Calculate()const
{
int 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 is : "<<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();
}