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


 

 

 

 

MODULE 16

FROM SINGLE TO C++ MULTI INHERITANCE 1

 

 

 

 

My Training Period: xx hours

 

The source code for this session is available in C++ Multi Inheritance source code.

 

The C++ inheritance programming abilities:

 

Able to understand and use:

 

 

16.1      Introduction

 

 

 

 

 

 

 

 

 

 

 

 

 

16.2      First Multiple Inheritance

  • Study the following program named mulinher1.cpp, it will reveal the definition of two very simple classes in lines 7 through 32 named moving_van and driver.

1.     // program mulinher1.cpp

2.     #include <iostream>

3.     using namespace std;

4.      

5.     // class declaration and implementation part

6.     // base class #1

7.     class  moving_van

8.     {

9.        protected:

10.            float  payload;

11.            float  gross_weight;

12.            float  mpg;

13.            // this variable only available for derived class

14.            // because of the protected keyword

15.     public:

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

17.            {

18.                   payload = pl;

19.                   gross_weight = gw;

20.                   mpg = input_mpg;

21.             };

22.            

23.             float  efficiency(void)

24.             {

25.                  return (payload / (payload + gross_weight));

26.              };

27.             

28.              float  cost_per_ton(float fuel_cost)

29.              {

30.                   return (fuel_cost / (payload / 2000.0));

31.              };

32.     };

33.   

34.  // base class #2

35.  class driver

36.  {

37.    protected:

38.            float   hourly_pay;

39.    public:

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

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

42.  };

43.   

44.  // derived class

45.  // inherit from two different base classes

46.  class  driven_truck : public  moving_van, public driver

47.  {

48.    public:

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

50.         {      payload = pl;

51.                gross_weight = gw;

52.                mpg = input_mpg;

53.                hourly_pay = pay;

54.          };

55.         

56.           float cost_per_full_day(float cost_of_gas)

57.           {

58.                return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg);

59.            };

60.    };

61.   

62.  // the main program

63.  int  main()

64.  {

65.     // instantiate objects…

66.     driven_truck   john_merc; 

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

68.   

69.     cout<<"The efficiency of the Merc truck is "<<john_merc.efficiency()<<

70.                                                                 " %\n";

71.     cout<<"The cost per mile for John to drive Merc is

72.                                        $"<<john_merc.cost_per_mile()<<"\n";

73.     cout<<"The cost per day for John to drive Merc is

74.                               $"<<john_merc.cost_per_full_day(1.129)<<"\n";

75.    

76.     // system("pause");

77.   

78.     return 0;

79.  }

 

79 Lines: Output:

 

C++ Multi inheritance program example

class driven_truck : public  moving_van, public driver

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)

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

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

 

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

cout<<"The cost per mile for John to drive Merc 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";

 

  • All of the rules for private or protected variables and public or private methods inheritance as used with single inheritance also extend to multiple inheritances.

16.3    Duplicate Method Names

  • You will notice that both of the base classes have a method named initialize(), and both of these are inherited into the derived class. However, if we attempt to send a message to one of these methods, we will have a problem, because the system does not know which one we are referring to. This problem will be solved as illustrated in the next program example.

  • Before going on to the next program example, it should be noted that we have not declared any objects of the two base classes in the main program.  Be sure to compile and run this program after you understand its operation completely.

  • The second program example named mulinher2.cpp, illustrates the use of classes with duplicate method names being inherited into a derived class.

 
1.     // program mulinher2.cpp
2.     #include <iostream>
3.     using namespace std;
4.      
5.     // declaration and implementation class part
6.     // base class #1
7.     class  moving_van
8.     {
9.        protected:
10.     	float   payload;
11.     	float   gross_weight;
12.     	float   mpg;
13.    public:
14.     	void  initialize(float pl, float gw, float input_mpg)
15.      	{
16.         		payload = pl;
17.         		gross_weight = gw;
18.         		mpg = input_mpg;
19.       	};
20.      	
21.      	float efficiency(void)
22.      	{
23.         		return (payload / (payload + gross_weight));
24.      	};
25.      	
26.      	float cost_per_ton(float fuel_cost)
27.      	{
28.         		return (fuel_cost / (payload / 2000.0));
29.      	};
30.      	
31.      	float cost_per_full_day(float  cost_of_gas)   //number one
32.      	{
33.         		return (8.0 * cost_of_gas * 55.0 / mpg);
34.      	};
35.       };
36.  
37. // base class #2
38. class   driver
39. {
40.    protected:
41.    	float   hourly_pay;
42.    public:
43.    	// same method name as in moving van class…
44.     	void   initialize(float pay) {hourly_pay = pay; };
45.     	float  cost_per_mile(void) {return (hourly_pay / 55.0); } ;
46.     	float  cost_per_full_day(float overtime_premium)   //number two
47.         	{return (8.0 * hourly_pay); };
48. };
49.  
50. // derived class
51. // notice also the duplicated method names used
52. class  driven_truck : public  moving_van, public driver
53. {
54.    public:
55.    	void initialize_all(float pl, float gw, float input_mpg, float pay)
56.       	{ 
57.         		payload = pl;
58.         		gross_weight = gw;
59.         		mpg = input_mpg;
60.         		hourly_pay = pay;
61.        	};
62.     	
63.     	float  cost_per_full_day(float  cost_of_gas)     // #3
64.     	{
65.         		return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg);
66.     	};
67. };
68.  
69. // the main program
70. int   main()
71. {
72.    	driven_truck   john_merc;
73.  	
74.    	john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50);
75.    	cout<<"The efficiency of the Merc is "<<john_merc.efficiency()<<" %\n";
76.    	cout<<"The cost per mile for John to drive is 
77.                                     $"<<john_merc.cost_per_mile()<<"\n\n";
78.  	
79.    	cout<<"   calling the appropriate method using:\n";
80.    	cout<<"   john_merc.moving_van::cost_per_full_day()\n";
81.    	cout<<"--------------------------------------------\n";
82.    	cout<<"The cost per day for the Merc is 
83.                 	$"<<john_merc.moving_van::cost_per_full_day(1.129)<<"\n\n";
84.  	
85.    	cout<<"   calling the appropriate method using:\n";
86.    	cout<<"   john_merc.driver::cost_per_full_day()\n";
87.    	cout<<"----------------------------------------\n";
88.    	cout<<"The cost of John for a full day is 
89.                     	$"<<john_merc.driver::cost_per_full_day(15.75)<<"\n\n";
90.  	
91.    	cout<<"   calling the appropriate method using:\n";
92.    	cout<<"   john_merc.driven_truck::cost_per_full_day()\n";
93.    	cout<<"----------------------------------------------\n";
94.    	cout<<"The cost per day for John to drive Merc is 
95.                 	$"<<john_merc.driven_truck::cost_per_full_day(1.129)<<"\n";
96.    	
97.    	// system("pause");
98.    	return 0;
99. }

 

99 Lines: Output:

 

C++ Multi inheritance duplicate method/function program example

cout<<"The cost per day for the Merc is $"<<john_merc.moving_van::cost_per_full_day(1.129)<<"\n\n";
...
cout<<"The cost of John for a full day is $"<<john_merc.driver::cost_per_full_day(15.75)<<"\n\n";
...
cout<<"The cost per day for John to drive Merc is $"<<john_merc.driven_truck::cost_per_full_day(1.129)<<"\n";
...

tenouk fundamental of C++ multi inheritance tutorial

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ multi inheritance reading:

 

  1. The source code is available in C++ Multi Inheritance source code.
  2. The Visual C++ .NET programming tutorials.
  3. Check the best selling C / C++ and object oriented books at Amazon.com.
  4. See MFC library class hierarchy chart here.
  5. Subjects, topics or books related to the following item:

 

    1. Object Oriented Design.
    2. Object Oriented Analysis.
    3. Standard Template Library.

 

  1. Typecasting that discussed in C/C++ Typecasting tutorial extensively deals with Inheritance and Multi inheritance.

 

 

 

 

 

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


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