Derived class call its base constructor : Destructor « Class « C++






Derived class call its base constructor

Derived class call its base constructor
 

#include <iostream>
using namespace std;

class BaseClass {
protected:
  int i;
public:
  BaseClass(int x) { 
     i = x; 
     cout << "Constructing base\n"; 
  }
  ~BaseClass() { 
     cout << "Destructing base\n"; 
  }
};

class DerivedClass: public BaseClass {
  int j;
public:
  DerivedClass(int x, int y): BaseClass(y) { 
     j = x; 
     cout << "Constructing DerivedClass\n"; 
  }

  ~DerivedClass() { 
     cout << "Destructing DerivedClass\n"; 
  }
  void show() { 
     cout << i << " " << j << endl; 
  }
};

int main()
{
  DerivedClass ob(3, 4);

  ob.show();

  return 0;
}


           
         
  








Related examples in the same category

1.Constructing and Destructing sequence for two base classesConstructing and Destructing sequence for two base classes
2.Derived constructor uses no parametersDerived constructor uses no parameters
3.Define and use the destructorDefine and use the destructor
4.System will call the destructorSystem will call the destructor
5.Implement a destructorImplement a destructor
6.Using a constructor and destructor.Using a constructor and destructor.
7.Define destrcuctor outside the class definition