Wednesday 25 February 2015

C++program to show the use of Constructor & Destructor

C++program to show the use of Constructor & Destructor

#include<iostream.h>
#include<conio.h> 

 class CAdd
 {

 public:
  int one;

  CAdd(int two)
  {
   cout << "A constructor is called" << endl;
   one=two;
  }

  CAdd()
  {
   cout << "A default constructor is called " << endl;
  }

  ~CAdd()
  {
   cout << "Destructing " << one << endl;
  }

  int add()
  {
   return(one+one);
  }
 };

 void main()
 {
  CAdd myobj1(4);
  CAdd myobj2;

  cout << myobj1.one << endl;
  cout << "Enter a number : " ;

  cin >> myobj2.one;
  cout << myobj2.add() << endl;

 getch()
 }

No comments:

Post a Comment