The single and multi-inheritance C++ program example: constructing the a simple family inheritance


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 the family inheritance using C++ single and multi inheritance programming

To show: How to construct family inheritance  tree using C++ single and multi inheritance  programming




// single and multiple inheritance C++ program example

// constructing the family inheritance tree structure

#include <iostream>

using namespace std;


// class declaration part

// the base class

class MyFather



char* EyeColor;

char* HairType;

double FamSaving;

int FamCar;




char* ShowEyeColor();

char* ShowHairType();

long double FamilySaving();

int FamilyCar();



// another base class

class MyMother


// notice the same member variables names/signatures as in MyFather class


char* EyeColor;

char* HairType;

int FamHouse;




char* ShowMotherEye();

char* ShowMotherHair();

int FamilyHouse();



// single inheritance derived/inherited class

class MySelf:public MyFather


// another member variables with same names/signatures

char* HairType;

char* Education;




char* ShowMyHair();

char* ShowMyEducation();



// multiple inheritance derived/inherited class

// notice the keyword public must follow every parent class list as needed

class MySister:public MyFather,public MyMother


char* SisEye;

float MonAllowance;




char* ShowSisEye();

float ShowSisAllownace();



// class implementation part

char* MyFather::ShowEyeColor()


return EyeColor = "Brown";



char* MyFather::ShowHairType()


return HairType = "Bald";



long double MyFather::FamilySaving()


return FamSaving = 100000L;



int MyFather::FamilyCar()


return FamCar = 4;



char* MyMother::ShowMotherEye()


return EyeColor = "Blue";



char* MyMother::ShowMotherHair()


return HairType = "Curly Blonde";



int MyMother::FamilyHouse()


return FamHouse = 3;



char* MySelf::ShowMyHair()


return HairType = "Straight Black";



char* MySelf::ShowMyEducation()


return Education = "Post Graduate";



char* MySister::ShowSisEye()


return SisEye = "Black";



float MySister::ShowSisAllownace()


return MonAllowance = 1000.00;



// main program

int main(void)


// instantiate the objects

MyFather ObjFat;

MyMother ObjMot;

MySelf ObjSelf;

MySister ObjSis;


cout<<"--My father's data (base class)--"<<endl;

cout<<"His eye: "<<ObjFat.ShowEyeColor()<<"\n"

<<"His hair: "<<ObjFat.ShowHairType()<<"\n"

<<"Family Saving: USD"<<ObjFat.FamilySaving()<<"\n"

<<"Family Car: "<<ObjFat.FamilyCar()<<" cars.\n";

cout<<"\n--My mother's data (base class)--"<<endl;

cout<<"Her eye: "<<ObjMot.ShowMotherEye()<<endl;

cout<<"Her hair: "<<ObjMot.ShowMotherHair()<<endl;

cout<<"Our family house: "<<ObjMot.FamilyHouse()<<" houses."<<endl;


// notice how to access the base/parent class member functions through the child or derived objects

cout<<"\n--My data (derived/inherited class)--"<<endl;

cout<<"My Hair: "<<ObjSelf. ShowMyHair()<<endl;

cout<<"My family saving: USD"<<ObjSelf.MySelf::FamilySaving()<<endl;

cout<<"My family car: "<<ObjSelf.MySelf::FamilyCar()<<" cars."<<endl;

cout<<"My education: "<<ObjSelf.ShowMyEducation()<<endl;


cout<<"\n--My sister's data (derived/inherited class)--"<<endl;

cout<<"Her eye: "<<ObjSis. ShowSisEye()<<endl;

cout<<"Our family saving: USD"<<ObjSis.MySister::FamilySaving()<<endl;

cout<<"Our family car: "<<ObjSis.MySister::FamilyCar()<<" cars."<<endl;

cout<<"Our family house: "<<ObjSis.MySister::FamilyHouse()<<" houses."<<endl;

cout<<"Her monthly allowances: USD"<<ObjSis.ShowSisAllownace()<<endl;


return 0;



Output example:


--My father's data (base class)--

His eye: Brown

His hair: Bald

Family Saving: USD100000

Family Car: 4 cars.


--My mother's data (base class)--

Her eye: Blue

Her hair: Curly Blonde

Our family house: 3 houses.


--My data (derived/inherited class)--

My Hair: Straight Black

My family saving: USD100000

My family car: 4 cars.

My education: Post Graduate


--My sister's data (derived/inherited class)--

Her eye: Black

Our family saving: USD100000

Our family car: 4 cars.

Our family house: 3 houses.

Her monthly allowances: USD1000

Press any key to continue . . .



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