include <iostream>
class factorial
{
long int fact=1;
factorial (int no) // This is a constructor with 1 argument
{
int store=no;
for ( ; no <1 ; --no)
fact*=no;
cout << "Factorial of " << store << "is " << fact;
}
}
int main()
{
int no;
factorial f1(3); // Direct call
// User choice
cout << "Enter a number : ";
cin >> no;
factorial f(no);
return 0;
}
#include<iostream>
using namespace std;
class A
{
int n;
public:
void call(int n)
{
cout<<"\nFibonacci numbers upto "<<n<<" are: \n";
int a(1),b(1),t(0);
for(int i=0;i<n;i++)
{
cout<<a<<" ";
t=b;
b=a+b;
a=t;
}
cout<<endl;
}
A()
{
cout<<"\nEnter the number of Fibonacci numbers you wish to calculate: ";
cin>>n;
call(n);
}
A(int n)
{
call(n);
}
};
int main()
{
A *a;
cout<<"\nDefault constructor call (Asks for user input):\n";
a = new A;
cout<<"\nParameterized constructor call (Assumes input as value passed):\n";
a = new A(10);
return 0;
}
Default constructor call (Asks for user input):
Enter the number of Fibonacci numbers you wish to calculate: 6
Fibonacci numbers upto 6 are:
1 1 2 3 5 8
Parameterized constructor call (Assumes input as value passed):
Fibonacci numbers upto 10 are:
1 1 2 3 5 8 13 21 34 55