A very simple family structure tree using the C++ inheritance program example

 

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: Constructing a very simple family structure tree using the C++ inheritance principles programming

To show: How to construct a very simple family structure tree using the C++ inheritance principle programming

 

// another working C++ inheritance example building a simple family tree

#include <iostream>

using namespace std;

 

// class declaration and implementation

// MyFather base class

class MyFather

{

// member variables and member functions

private:

char* EyeColor;

char* HairType;

double FamSaving;

protected:

public:

MyFather(){}

~MyFather(){}

char* GetEye()

{ return EyeColor = "Brown";}

char* GetHair()

{ return HairType = "Straight";}

double GetSaving()

{return FamSaving = 30000;}

};

 

// MySelf derived class with public inheritance

class MySelf:public MyFather

{

// same as normal class actually

private:

char* MyEye;

char* MyHair;

public:

MySelf(){}

~MySelf(){}

char* GetMyEye()

{ return MyEye = "Blue";}

char* GetMyHair()

{return MyHair = "Curly";}

protected:

};

// MySister derived class with public inheritance

class MySister:public MyFather

{

private:

char* SisEye;

char* SisHair;

public:

MySister(){}

~MySister(){}

char* GetSisEye()

{return SisEye = "Black";}

char* GetSisHair()

{ return SisHair = "Blonde";}

};

 

// main program

int main(void)

{

// base class object instantiation

MyFather Test1;

 

cout<<"Testing the C++ inheritance program example"<<endl<<endl;

cout<<"My father's eye is = "<<Test1.GetEye()<<endl;

cout<<"My father's hair is = "<<Test1.GetHair()<<endl;

 

// derived class object instantiation

MySelf Test2;

 

cout<<"\nMy eye is = "<<Test2.GetMyEye()<<endl;

cout<<"My hair is = "<<Test2.GetMyHair()<<endl;

 

// the following are inherited from MyFather class

cout<<"Our family saving is = $"<<Test2.GetSaving()<<endl;

cout<<"My father's eye is = "<<Test2.GetEye()<<endl;

cout<<"My father's hair is = "<<Test2.GetHair()<<endl;

 

// another derived class object instantiation

MySister Test3;

 

cout<<"\nMy sister's eye is = "<<Test3.GetSisEye()<<endl;

cout<<"My sister's hair is = "<<Test3.GetSisHair()<<endl;

 

// the following are inherited from MyFather class

cout<<"Our family saving is = $"<<Test3.GetSaving()<<endl;

cout<<"My father's eye is = "<<Test3.GetEye()<<endl;

cout<<"My father's hair is = "<<Test3.GetHair()<<endl<<endl;

 

return 0;

}

 

Output example:

 

Testing the C++ inheritance program example

 

My father's eye is = Brown

My father's hair is = Straight

 

My eye is = Blue

My hair is = Curly

Our family saving is = $30000

My father's eye is = Brown

My father's hair is = Straight

 

My sister's eye is = Black

My sister's hair is = Blonde

Our family saving is = $30000

My father's eye is = Brown

My father's hair is = Straight

Press any key to continue . . .

 

 

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