|< C++ Inheritance 1 | Main | C++ Inheritance 2 >| Site Index | Download |


 

 

 

 

 

MODULE 14a

C++ OBJECT INHERITANCE EXAMPLES 1a

 

 

 

 

 

 

 

 

 

My Training Period: yy hours.

 

1.4    Using All The Classes

  • Examine the program named allvehicle.cpp carefully.  This main program uses all three of the classes we have been discussing in this Module.

  • Before compiling and running this program, make sure these header files: vehicle.h, vehicle.cpp, car.h, car.cpp, truck.h, truck.cpp and allvehicle.cpp are in one project.  Also, you have to compile without error the implementation part: vehicle.cpp, car.cpp and truck.cpp files, generating object files.  Compile and execute the allvehicle.cpp main program.

  • Also make sure the first main() program transprt.cpp not in this project.  We only need one main() program.

1.     // the new main program for inheritance, allvehicle.cpp

2.     // compile and run this program

3.      

4.     #include  <iostream>

5.     using namespace std;

6.     #include  "vehicle.h"  // the interface of the Cvehicle class

7.     #include  "car.h"        // the interface of the Ccar class

8.     #include  "truck.h"     // the interface of the Ctruck class

9.      

10.  void main()

11.  {

12.     Cvehicle   unicycle;  // the base class

13.   

14.     cout<<"Unicycle using the Cvehicle base class\n";

15.     cout<<"--------------------------------------\n";

16.   

17.      unicycle.initialize(1,12.5);

18.   

19.     cout<<"Unicycle has "<<unicycle.get_wheels()<<" tire.\n";

20.     cout<<"Unicycle wheel load is "<<unicycle.wheel_load()<<"kg on one

21.                                                                  tire.\n";

22.     cout<<"Unicycle weight is "<<unicycle.get_weight()<<" kg.\n\n";

23.   

24.      Ccar  sedan;   // the derived class

25.   

26.     cout<<"Sedan car using the Ccar derived class\n";

27.     cout<<"--------------------------------------\n";

28.   

29.           sedan.initialize(4,3500.0,5);

30.   

31.     cout<<"Sedan car carries "<<sedan.passengers()<<" passengers.\n";

32.     cout<<"Sedan car weight is "<<sedan.get_weight()<<" kg.\n";

33.     cout<<"Sedan car wheel load is "<<sedan.wheel_load()<<"kg per tire.\n\n";

34.   

35.           Ctruck  trailer; // the derived class

36.   

37.     cout<<"Trailer using the Ctruck derived class\n";

38.     cout<<"--------------------------------------\n";

39.   

40.        trailer.initialize(18,12500.0);

41.        trailer.init_truck(1,33675.0);

42.   

43.     cout<<"Trailer weight is "<<trailer.get_weight()<<" kg.\n";

44.     cout<<"Trailer efficiency is "<<100.00 * trailer.efficiency()<<"%.\n";

45.    

46.     // system("pause");

47.   }

 

47 Lines:Output:

 

C++ inheritance program output example

 

Program 14.8:  allvehicle.cpp, the main program

#include  "vehicle.h"   // the interface of the Cvehicle class

#include  "car.h"         // the interface of the Ccar class

#include  "truck.h"      // the interface of the Ctruck class

1.5    The  #ifndef ... #endif

#ifndef  VEHICLE_H    // preprocessor directive

#define  VEHICLE_H

...

...

...

#endif

C++ object class hierarchy, based and derived class inheritance

 

C++ inheritance - a simple but detail C++ class hierarchy illustration

 

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

More Working Program Examples and Experiments

// inheritance

#include <iostream>

using namespace std;

 

// a base class

class Base

{

       // member variables and member functions...

       public:

           Base(){}

           ~Base(){}

       protected:

       private:

};

 

// a derived class...

class Derived:public Base

{

       // same as normal class actually...

       // member variables and member function...

       public:

           Derived(){}

           ~Derived(){}

       private:

       protected:

};

 

void main()

{

       cout<<"Testing the program skeleton..."<<endl;

}

 

Output:

 

a very simple C++ inheritance program example output

// an inheritance

#include <iostream>

using namespace std;

 

// a class declaration and implementation

// a base class...

class MyFather

{

       // member variables and member functions...

       private:    // the private keyword is optional

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

};

 

// a derived class...

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:

};

 

// another derived class...

class MySister:public MyFather

{

       private:        // the private keyword is optional

           char* SisEye;

           char* SisHair;

 

       public:

           MySister(){}

           ~MySister(){}

           char* GetSisEye()

           {return SisEye = "Black";}

           char* GetSisHair()

           { return SisHair = "Blonde";}

};

 

// the main program

int main()

{

  // base class object...

  MyFather Test1;

 

  cout<<"Testing the inheritance program...\n"<<endl;

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

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

 

  // derived class object...

  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...

  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()<<"\n\n";

  return 0;

}

 

Output:

 

C++ inheritance program example output samples

// inheritance

#include <iostream>

using namespace std;

 

// class declaration and implementation

// a base class...

class MyFather

{

       // member variables and member functions...

       private:        // the private keyword is optional

           char* EyeColor;

           char* HairType;

           double FamSaving;

       

       protected:

       // protected members here…

      

       public:

           MyFather(){}

           ~MyFather(){}

           char* GetEye()

           { return EyeColor = "Brown";}

           char* GetHair()

           { return HairType = "Straight";}

           double GetSaving()

           {return FamSaving = 30000;}

};

 

//derived class...

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:

};

 

//another derived class...

class MySister:public MyFather

{

     private:

     char* SisEye;

     char* SisHair;

 

     public:

     MySister(){}

     ~MySister(){}

     char* GetSisEye()

     {return SisEye = "Black";}

     char* GetSisHair()

     { return SisHair = "Blonde";}

};

 

// the main program

int main()

{

  // base class object...

  MyFather Test1;

 

  cout<<"Testing the inheritance program...\n"<<endl;

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

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

 

  // derived class object...

  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...

  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()<<"\n\n";

  return 0;

}

 

Output:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

 

C++ full working inheritance program example - a sample program output

// **************herit2.cpp**************

// **************FEDORA 3, g++ x.x.x****

#include <iostream>

using namespace std;

 

// a class declaration and implementation

// base class...

class MyFather

{

                // member variables and member functions...

                private:    // the private keyword is optional

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

};

 

// derived class...

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:

                     //...

};

 

// another derived class...

class MySister:public MyFather

{

                private:

                    char* SisEye;

                    char* SisHair;

               

                public:

                    MySister(){}

                    ~MySister(){}

                    char* GetSisEye()

                    {return SisEye = "Black";}

                    char* GetSisHair()

                    {return SisHair = "Blonde";}

};

 

// the main program

int main()

{

      // base class object...

      MyFather Test1;

    

      cout<<"Testing the inheritance program...\n"<<endl;

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

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

    

      // derived class object...

      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...

      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()<<"\n\n";

      return 0;

}

 

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

[bodo@bakawali ~]$ ./herit2

 

Testing the inheritance program...

 

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

 

tenouk C++ object inheritance program examples

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ inheritance related reading:

 

  1. The source code for this tutorial is available in C++ Inheritance source codes.
  2. Check the best selling C / C++ and object oriented books at Amazon.com.
  3. See MFC library class hierarchy chart here.
  4. The Visual C++ .NET.
  5. Find a lot of information about C++ history and evolution at Bjarne Stroustrup site.

 

 

 

 

 

 

 

|< C++ Inheritance 1 | Main | C++ Inheritance 2 >| Site Index | Download |


C++ Class Inheritance:  Part 1 | Part 2 |