Call base's constructor and destructor from derived class : derived class « Class « C++ Tutorial






#include <iostream> 
using namespace std; 
 
class BaseClass { 
public: 
  BaseClass() { cout << "Constructing base portion\n"; } 
  ~BaseClass() { cout << "Destructing base portion\n"; } 
}; 
 
class DerivedClass: public BaseClass { 
public: 
  DerivedClass() { cout << "Constructing derived portion\n"; } 
  ~DerivedClass() { cout << "Destructing derived portion\n"; } 
}; 
 
int main() 
{ 
  DerivedClass ob; 
 
  // do nothing but construct and destruct ob 
 
  return 0; 
}
Constructing base portion
Constructing derived portion
Destructing derived portion
Destructing base portion








9.18.derived class
9.18.1.Add a constructor to derived class
9.18.2.Call base's constructor and destructor from derived class
9.18.3.Access member of base and use member of derived class
9.18.4.Overriding a base class method in a derived class
9.18.5.Hiding methods
9.18.6.Calling base method from overridden method.
9.18.7.Using virtual methods
9.18.8.Inherit base class twice
9.18.9.dynamic_cast: Cast from Derived * to Base *