< C++ Multi Inheritance 1a |Main | C++ Polymorphism >|Site Index |Download >


 

 

 

 

MODULE 16a

MORE ON C++ OBJECT ORIENTED: MULTI INHERITANCE 2

 

 

 

 

My Training Period: xx  hours

 

This is a continuation from previous Module and a final part, more working program examples. The source code is available in C++ Multi Inheritance source code.

 

The C++ multi inheritance programming knowledge that supposed to be acquired:

 

Able to understand and use:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

More Working Program Examples and Experiments

  • Let have program examples demonstrating the multi inheritance.  The simple class hierarchy for this program example is illustrated below.

 

C++ Multi inheritance class objects illustration

 

// multiple inheritance program example...

#include <iostream>

using namespace std;

 

// a class declaration part

// the base class....

class MyFather

{

  protected:

       char* EyeColor;

       char* HairType;

       double FamSaving;

       int FamCar;

 

  public:

       MyFather(){}

       ~MyFather(){}

       char* ShowEyeColor();

       char* ShowHairType();

       long double FamilySaving();

       int FamilyCar();

};

 

// another base class...

class MyMother

{

  // notice the same member variables names

  // as in MyFather class...

  protected:

       char* EyeColor;

       char* HairType;

       int FamHouse;

 

  public:

       MyMother(){}

       ~MyMother(){}

       char* ShowMotherEye();

       char* ShowMotherHair();

       int FamilyHouse();

};

 

// single inheritance derived class...

// aaahhh!!! my class :-) finally!!!

class MySelf:public MyFather

{

  // another member variables with same names...

  char* HairType;

  char* Education;

 

  public:

       MySelf(){ }

       ~MySelf(){ }

       char* ShowMyHair();

       char* ShowMyEducation();

};

 

// multiple inheritance derived class...

// notice the keyword public must follow every

// parent class list as needed...

class MySister:public MyFather,public MyMother

{

  char* SisEye;

  float MonAllowance;

 

  public:

       MySister(){}

       ~MySister(){}

       char* ShowSisEye();

       float ShowSisAllownace();

};

 

// a 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;}

 

// the main program

int main()

{

     // instantiate the objects...

      MyFather ObjFat;

      MyMother ObjMot;

      MySelf ObjSelf;

      MySister ObjSis;

     

      cout<<"--My father's data--"<<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--"<<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--"<<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--"<<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:

 

C++ Multi inheritance program example

 

 

C++ Multi inheritance class illustration

 

// another simple multiple

// inheritance program example...

#include <iostream>

using namespace std;

 

// class declaration and implementation part

// base class...

class Base1

{

  protected:

      int SampleDataOne;

  public:

      Base1(){SampleDataOne = 100;}

      ~Base1(){}

     

      int SampleFunctOne()

      {return SampleDataOne;}

};

 

// another base class...

class Base2

{

  protected:

      int SampleDataTwo;

  public:

      Base2(){SampleDataTwo = 200;}

      ~Base2(){}

     

      int SampleFunctTwo()

      {return SampleDataTwo;}

};

 

// derived class...

class Derived1:public Base1,public Base2

{

  int MyData;

  public:

      Derived1(){MyData = 300;}

      ~Derived1(){}

      int MyFunct()

      {

           // the protected data of the base classes are available

           // for this derived class...

           return (MyData + SampleDataOne + SampleDataTwo);

  }

};

 

// the main program

int main()

{

  // instantiate objects...

  Base1 SampleObjOne;

  Base2 SampleObjTwo;

  Derived1 SampleObjThree;

 

  cout<<"Normal access Base1 class data: "<<SampleObjOne.SampleFunctOne()<<endl;

  cout<<"Normal access Base2 class data: "<<SampleObjTwo.SampleFunctTwo()<<endl;

  cout<<"Normal access Derived1 class data: "<<SampleObjThree.MyFunct()<<endl;

  cout<<"\nExtracting the Base1 data through the derived class:"<<endl;

  cout<<"Base1 data: "<<SampleObjThree.Base1::SampleFunctOne()<<endl;

  cout<<"\nExtracting the Base2 data through the derived class:"<<endl;

  cout<<"Base2 data: "<<SampleObjThree.Base2::SampleFunctTwo()<<endl;

 

  return 0;

}

 

Output:

 

C++ Inheritance using Visual C++ program example

 

------------------------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

// program mulinher3.cpp

#include <iostream>

usingnamespace std;

 

// class declaration and implementation part

// base class #1

class   moving_van

{

  protected:

      double   payload;

      double   weight;      //note this variable

      double   mpg;

  public:

      void  initialize(double pl, double gw, double input_mpg)

       {

          payload = pl;

          weight = gw;       

          mpg = input_mpg;

        };

       double  efficiency(void)

        { return(payload / (payload + weight)); };

       double  cost_per_ton(double fuel_cost)

        { return (fuel_cost / (payload / 2000.0)); };

};

 

// base class #2

class  driver

{

  protected:

      double  hourly_pay;

      double  weight;         // another weight variable

      // variable with same name as in class number one

  public:

      void  initialize(double  pay, double  input_weight)

      // same method name but different number of parameter

       {

              hourly_pay = pay;

              weight = input_weight;

       };

      double  cost_per_mile(void) {return (hourly_pay / 55.0); };

      double  drivers_weight(void) {return (weight); };

};

    

// derived class with multi inheritance

// declaration and implementation

class  driven_truck : public  moving_van,public driver

{

 public:

     void initialize_all(double pl, double gw, double input_mpg, double pay)

      // another same method name but different number of parameter

      {

        payload = pl;

        moving_van::weight = gw;

        mpg = input_mpg;

        hourly_pay = pay;

    };

   

   double  cost_per_full_day(double cost_of_gas)

    {  return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg); };

   

   double  total_weight(void)

   // see, how to call different variables with same name

   {

       cout<<"\nCalling appropriate member variable\n";

       cout<<"---->(moving_van::weight)+(driver::weight)\n";

       cout<<"------------------------------------------\n";

      return ((moving_van::weight) + (driver::weight));

    };

};

    

// the main program

int   main()

{

  driven_truck   john_merc;

 

  john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50);

  // accessing the derived class method

  john_merc.driver::initialize(15.50, 250.0);

  // accessing the base class number two

    

  cout<<"The efficiency of the Merc is "<<john_merc.efficiency()<<" %\n";

  cout<<"The cost per mile for John to drive is $"<<john_merc.cost_per_mile()<<"\n";

  cout<<"The cost per day for John to drive Merc is $"<<john_merc.cost_per_full_day(1.129)<<"\n";

  cout<<"The total weight is "<<john_merc.total_weight()<<" ton\n";

 return  0;

}

 

Output:

 

C++ inheritance using Microsoft Visual C++

//////- multiherit.cpp-/////////

//////-multi inheritance-///////

#include <iostream>

using namespace std;

 

// declaration and implementation class part

// base class #1

class moving_van

{

   protected:

       float   payload;

       float   gross_weight;

       float   mpg;

      

   public:

        void initialize(float pl, float gw, float input_mpg)

        {

            payload = pl;

            gross_weight = gw;

            mpg = input_mpg;

        };

       

        float efficiency(void)

        {return (payload / (payload + gross_weight));};

       

        float cost_per_ton(float fuel_cost)

        {return (fuel_cost / (payload / 2000.0));};

       

        float cost_per_full_day(float cost_of_gas)   // #1

        {return (5.5 * cost_of_gas * 55.0 / mpg);};

};

 

// base class #2

class driver

{

   protected:

       float   hourly_pay;

      

   public:

      // same method name as in moving van class.

       void initialize(float pay) {hourly_pay = pay;};

       float cost_per_mile(void) {return (hourly_pay / 55.0); } ;

       float cost_per_full_day(float overtime_premium)   // #2

       {return (7.0 * hourly_pay); };

};

 

// derived class

// notice also the duplicated method names used

class driven_truck : public moving_van, public driver

{

public:

    void initialize_all(float pl, float gw, float input_mpg, float pay)

    {

          payload = pl;

          gross_weight = gw;

          mpg = input_mpg;

          hourly_pay = pay;

    };

    

    float cost_per_full_day(float cost_of_gas)     // #3

    {

           return ((7.0 * hourly_pay) + (5.5 * cost_of_gas * 55.0) / mpg);

    };

};

 

// main program

int main()

{

   driven_truck   john_merc;

 

   john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50);

   cout<<"Merc's efficiency is "<<john_merc.efficiency()<<" %\n";

   cout<<"Cost per mile for John to drive is USD"<<john_merc.cost_per_mile()<<"\n\n";

   cout<<"Calling the appropriate method using:\n";

   cout<<"john_merc.moving_van::cost_per_full_day()\n";

   cout<<"-----------------------------------------\n";

   cout<<"Merc's cost per day is USD"<<john_merc.moving_van::cost_per_full_day(1.129)<<"\n\n";

   cout<<"Calling the appropriate method using:\n";

   cout<<"john_merc.driver::cost_per_full_day()\n";

   cout<<"----------------------------------------\n";

   cout<<"John's full day cost is USD"<<john_merc.driver::cost_per_full_day(15.75)<<"\n\n";

   cout<<"Calling the appropriate method using:\n";

   cout<<"john_merc.driven_truck::cost_per_full_day()\n";

   cout<<"----------------------------------------------\n";

   cout<<"Merc's cost per day for John to drive is USD"<<john_merc.driven_truck::cost_per_full_day(1.129)<<"\n";

 

   return 0;

}

 

[bodo@bakawali ~]$ g++ multiherit.cpp -o multiherit

[bodo@bakawali ~]$ ./multiherit

 

Merc's efficiency is 0.625 %

Cost per mile for John to drive is USD0.227273

 

Calling the appropriate method using:

john_merc.moving_van::cost_per_full_day()

-----------------------------------------

Merc's cost per day is USD65.6774

 

Calling the appropriate method using:

john_merc.driver::cost_per_full_day()

----------------------------------------

John's full day cost is USD87.5

 

Calling the appropriate method using:

john_merc.driven_truck::cost_per_full_day()

----------------------------------------------

Merc's cost per day for John to drive is USD153.177

 

tenouk C++ object oriented tutorial: multi-inheritance

 

 

 

 

 

 

 

 

Further C++ multi inheritance reading:

 

  1. The source code is available inC++ Multi Inheritance source code.
  2. Check the best selling C / C++ and object oriented books at Amazon.com.
  3. See MFC library class hierarchy char there.
  1. The VisualC++ .NET programming tutorials.
  2. Typecasting that discussed inC/C++ Typecasting tutorial extensively deals with Inheritance and Multi inheritance.

 

 

 

 

|< C++ Multi Inheritance 1a |Main | C++ Polymorphism >|Site Index |Download |


C++ Multi Inheritance:  Part 1 | Part 2 | Part 3