In C++ Constructors are special class functions which performs initialization of every object, Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object.

Program:


#include<iostream>

using namespace std;

class factorial

{

 public:

 long int i,f;

 factorial (int k)

 {

 f=k;

 cout<<"\n\n\n\t\t\tConstructor object is invoked";

 }

 ~factorial()

 {

  cout<<"\n\n\n\t\t\tConstructor is destroyed:";

  }

  void factor (int n);

  };

  void factorial::factor (int n)

  {

  for(i=1;i<=n;i++)

  {

  f*=i;

  }

  cout<<"\n\n\n\t\t\tThe factorial is :"<<f<<endl;

  }

  int main()

  {

  long x,y=1;

  cout<<"\n\n\n\t\t\tConstructor and destruction";

  cout<<"\n\n\n\t\t\t*************************";

  factorial f1(y);

  cout<<"\n\n\n\t\t\tEnter value for n:";

  cin>>x;

  f1.factor(x);

  return 0;

  }



OUTPUT:




BY
     REGU RAM SV.

Post a Comment

أحدث أقدم