Write a program to perform All type of Constructor with destructor

/*All type of Constructor with destructor */
#include<iostream>
using namespace std;
class cons
{
   int a,b;

   public:

   /* Default Constructor */
   cons()
   {
      a=90;
      b=5;
   }

   /* Parameterized Constructor */
   cons(int a, int b)
   {
     (*this).a=a;
     (*this).b=b;
   }
   /* Copy Constructor */

   cons(cons &old)
   {
     a=old.a;
     b=old.b;
   }

   /* Destructor */
   ~cons()
   {
     cout<<"\n\n a= "<<a;
     cout<<"\n\n b= "<<b;
  }

   void print()
   {
     cout<<"\n a= "<<a<<endl;
     cout<<"\n b= "<<b<<endl;
   }
};

int main()
{
  cons a(10,20);
  cons a1(a);
  cons a2;

  cout<<"\n\n Default Constructor";
  a2.print();

  cout<<"\n\n Parameterized Constructor ";
  a.print();

  cout<<"\n\n Copy Constructor";
  a1.print();

  cout<<"\n\n Destructor ";
  return 0;
}

Previous
Next Post »