The C++ with class inheritance demonstrating the constructor and destructor execution flow, function/method overloading and the use of private, protected and public keywords
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: Demonstrating the C++ class inheritance constructor and destructor execution flow and the use of private, protected and public keywords in C++ programming
To show:How the constructor and destructor execution flow and the use of private, protected and public keywords in C++ programming for C++ class inheritance
// C++ class inheritance again. Notice the sequence of the constructor
// and destructor execution, and private, public, protected usage
#include <iostream>
using namespace std;
class Base
{
// available for this class member functions ONLY
private:
int BaseVar;
int NewX;
int ExtraBaseVar;
// available to this and derived classes
protected:
int BaseVarOne;
// available to the derived and outside classes
public:
Base();
Base(int NewX);
~Base();
public:
int SetBaseData();
int ShowBaseData(){return BaseVar;}
int SimilarNameFunct();
};
class DerivedOne:public Base
{
// available to this class member functions ONLY
private:
int DerivedOneVar;
int ExtraDerivedVar;
// available to the derived and outside classes
public:
DerivedOne();
~DerivedOne();
// available to the derived and outside classes
public:
void SetDerivedOneData();
int ShowDerivedOneData()
{
// BaseVarOne is base class protected member
// variable, available to this derived class
return (DerivedOneVar + BaseVarOne);
}
int SimilarNameFunct();
};
// base class constructor
Base::Base()
{
BaseVar = 100;
//constructor counter
static int p;
cout<<"Invoking base class constructor #"<<p<<endl;
p++;
}
// another base class constructor
Base::Base(int)
{
// constructor counter
static int t;
cout<<"Invoking the 2nd base class constructor #"<<t<<endl;
t++;
BaseVar = NewX;
}
// base class member function
int Base::SetBaseData()
{
return NewX = 230;
}
int Base::SimilarNameFunct()
{
return ExtraBaseVar = 170;
}
// base class destructor
Base::~Base()
{
// destructor counter
static int q;
cout<<"Invoking the base class destructor #"<<q<<endl;
q++;
}
// derived class constructor
DerivedOne::DerivedOne()
{
DerivedOneVar = 200;
// this member variable is inherited from protected base class
BaseVarOne = 250;
// constructor counter
static int r;
cout<<"Invoking derived class constructor #"<<r<<endl;
r++;
}
// derived class destructor
DerivedOne::~DerivedOne()
{
// destructor counter
static int s;
cout<<"Invoking derived class destructor #"<<s<<endl;
s++;
}
void DerivedOne::SetDerivedOneData()
{}
// same member function/method name as base class
// it is valid since they are from different class
int DerivedOne::SimilarNameFunct()
{
return ExtraDerivedVar = 260;
}
void main(void)
{
// instantiate objects with class types
Base ObjOne, ObjFour;
DerivedOne ObjTwo, ObjFive;
Base ObjThree;
cout<<"\nBase class data = "<<ObjOne.ShowBaseData()<<endl;
cout<<"SimilarNameFunct() of base class = "<<ObjFour.SimilarNameFunct()<<endl;
cout<<"DerivedOne class data = "<<ObjTwo.ShowDerivedOneData()<<endl;
cout<<"SimilarNameFunct() of derived class = "<<ObjFive.SimilarNameFunct()<<endl;
cout<<"Another base class data = "<<ObjThree.SetBaseData()<<"\n\n";
return;
}
Output example:
Invoking base class constructor #0
Invoking base class constructor #1
Invoking base class constructor #2
Invoking derived class constructor #0
Invoking base class constructor #3
Invoking derived class constructor #1
Invoking base class constructor #4
Base class data = 100
SimilarNameFunct() of base class = 170
DerivedOne class data = 450
SimilarNameFunct() of derived class = 260
Another base class data = 230
Invoking the base class destructor #0
Invoking derived class destructor #0
Invoking the base class destructor #1
Invoking derived class destructor #1
Invoking the base class destructor #2
Invoking the base class destructor #3
Invoking the base class destructor #4
Press any key to continue . . .