A very simple C++ class object inheritance program example showing the default classes constructors and destructors

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Viewing the default classes constructors and destructors execution flow in C++ inheritance programming

To show: How the default classes constructors and destructors execution flow implemented in C++ inheritance programming

 

// a very simple C++ inheritance with default constructors

#include <iostream>

using namespace std;

// base class

class Base

{

// member variables & methods

public:

Base(){cout<<"Base class constructor"<<endl;}

~Base(){cout<<"Base class destructor"<<endl;}

protected:

private:

};

// derived class

class Derived:public Base

{

// same as normal class actually...

// member variables and member function...

public:

Derived(){cout<<"Derived class destructor"<<endl;}

~Derived(){cout<<"Derived class destructor"<<endl;}

private:

protected:

};

void main()

{

Base TestBaseObject;

Derived TestDerivedObject;

cout<<" Testing the C++ inheritance source code..."<<endl;

}

 

Output example:

 

Base class constructor

Base class constructor

Derived class destructor

Testing the C++ inheritance source code...

Derived class destructor

Base class destructor

Base class destructor

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index