Order Of Construction Of Derived Classes
Table of Contents

Introduction

Order of construction that happens when a derived class is instantiated.

Examples

Example 1: A base class and a derived class.

class Base
{
public:
    int m_nValue;
 
    Base(int nValue=0)
        : m_nValue(nValue)
    {
       cout << "Creating a Base object" << endl;
    }
};
 
class Derived: public Base
{
public:
    double m_dValue;
 
    Derived(double dValue=0.0)
        : m_dValue(dValue)
    {
      cout << "Creating a Derived object" << endl;
    }
};
 
int main()
{
  Base cBase; // allocates memory for Base 
  Derived cDerived; // C++ starts with the base portion of the class and then walks through
//the inheritance tree and constructs each derived portion of the class.
 
  return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.