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


 

 

 

 

MODULE 16_1

FROM SINGLE TO C++ MULTI INHERITANCE 1a

 

 

 

 

 

 

 

 

 

 

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 duplicated member variable names.

 

 

 

 

16.4   Duplicated Variable Names

  • Examine the following program example named mulinher3.cpp, you will notice that each base class has a variable with the same name, named weight.

1.     // program mulinher3.cpp

2.     #include <iostream>

3.     using namespace std;

4.      

5.     // class declaration and implementation part

6.     // class #1

7.     class   moving_van

8.     {

9.        protected:

10.         float   payload;

11.         float   weight;       // note this variable

12.         float   mpg;

13.     public:

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

15.         {

16.            payload = pl;

17.            weight = gw;       

18.            mpg = input_mpg;

19.          };

20.         

21.          float  efficiency(void)

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

23.         

24.          float  cost_per_ton(float fuel_cost)

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

26.  };

27.   

28.  // class #2

29.  class  driver

30.  {

31.     protected:

32.         float  hourly_pay;

33.         float  weight;          // another weight variable

34.         // variable with same name as in class number one

35.     public:

36.         void  initialize(float  pay, float  input_weight)

37.         // same method name but different number of parameter

38.         {

39.            hourly_pay = pay;

40.            weight = input_weight;

41.         };

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

43.         float  drivers_weight(void) {return (weight); };

44.  };

45.   

46.  // derived class with multi inheritance

47.  // declaration and implementation

48.  class  driven_truck : public  moving_van, public driver

49.  {

50.    public:

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

52.        // another same method name but different number of parameter

53.        {

54.              payload = pl;

55.              moving_van::weight = gw;

56.              mpg = input_mpg;

57.              hourly_pay = pay;

58.          };

59.         

60.          float  cost_per_full_day(float cost_of_gas)

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

62.         

63.          float  total_weight(void)

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

65.          {

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

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

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

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

70.          };

71.  };

72.   

73.  // the main program

74.  int   main()

75.  {

76.        driven_truck   john_merc;

77.      

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

79.        // accessing the derived class method

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

81.        // accessing the base class number two

82.      

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

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

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

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

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

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

89.       

90.        // system("pause");

91.        return  0;

92.  }

 

92 Lines: Output:

 

C++ Multi inheritance duplicate variable program example

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

16.5  Parameterized Types

16.6    Template – A Function Template

1.                  // using function template
2.                  #include <iostream>
3.                  using namespace std;
4.                   
5.                  // template declaration
6.                  template
7.                           <class ANY_TYPE> ANY_TYPE maximum(ANY_TYPE a, ANY_TYPE b)
8.                  {
9.                     return (a > b) ? a : b;
10.               }
11.                
12.               // the main program
13.               int  main(void)
14.               {
15.               int    x = 10, y = -9;
16.               float  real = 3.1415;
17.               char   ch = 'C';
18.                
19.               cout<<"maximum("<<x<<", "<<y<<")     = "<<maximum(x, y)<<"\n";
20.               cout<<"maximum(-47, "<<y<<")     = "<<maximum(-47,y)<<"\n";
21.               cout<<"maximum("<<real<<", "<<float(y)<<") = 
22.                                                         "<<maximum(real,float(y))<<"\n";
23.               cout<<"maximum("<<real<<", "<<float(x)<<") = 
24.                                                         "<<maximum(real,float(x))<<"\n";
25.               cout<<"maximum("<<ch<<", "<<'A'<<")     = "<<maximum(ch, 'A')<<"\n";
26.               
27.               // system("pause");
28.                
29.               return 0;
30.               }

               

30 Lines: Output:

 

C++ Multi inheritance function template program example

// template declaration
template <class ANY_TYPE> ANY_TYPE maximum(ANY_TYPE a, ANY_TYPE b)
{
   return (a > b) ? a : b;
}
  • This type can be replaced by any type which can be used in the comparison operation in line 9. If you have defined a class, and you have overloaded the operator ">", then this template can be used with objects of your class. Thus, you don't have to write a maximum function for each type or class in your program.

  • This function is included automatically for each type it is called with in the program, and the code itself should be very easy to understand.

  • You should realize that nearly the same effect can be achieved through the use of a macro, except that when a macro is used, the strict type checking is not done. Because of this and because of the availability of the inline function capability in C++, the use of macros is essentially been considered non-existent by experienced programmers.

16.7    Template - A Class template

  • The following program example named template2.cpp provides a template for an entire class rather than a single function. The template code is given in lines 8 through 17 and actually it is an entire class definition.

 

1.     // a class template

2.     #include   <iostream>

3.     using namespace std;

4.      

5.     const  int  MAXSIZE = 128;

6.      

7.     // a class template

8.     template  <class   ANY_TYPE>  class  stack

9.     {

10.         ANY_TYPE  array[MAXSIZE];

11.         int  stack_pointer;

12.         public:

13.             stack(void)   { stack_pointer = 0; };

14.             void push(ANY_TYPE input_data){ array[stack_pointer++] = input_data; };

15.             ANY_TYPE pop(void)    { return  array[--stack_pointer]; };

16.             int empty(void)       { return  (stack_pointer == 0); };

17.  };

18.   

19.  char  name[ ] = "Testing, this is an array, name[ ]";

20.   

21.  // the main program

22.  int   main(void)

23.  {

24.      int     x = 30, y = -10;

25.      float   real = 4.2425;

26.      

27.      stack<int>      int_stack;

28.      stack<float>    float_stack;

29.      stack<char *>   string_stack;

30.      

31.      // storing data

32.      int_stack.push(x);

33.      int_stack.push(y);

34.      int_stack.push(67);

35.      

36.      float_stack.push(real);

37.      float_stack.push(-20.473);

38.      float_stack.push(107.03);

39.      

40.      string_stack.push("This is the first line of string");

41.      string_stack.push("This is the second line of string");

42.      string_stack.push("This is the third line of string");

43.      string_stack.push(name);

44.      

45.      // displaying data

46.      cout<<"---------------Displaying data--------------------\n";

47.      cout<<"\nInteger stack\n";

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

49.      cout<<"Access using int_stack.pop(), first time : "<<int_stack.pop()<<"\n";

50.      cout<<"Access using int_stack.pop(), second time: "<<int_stack.pop()<<"\n";

51.      cout<<"Access using int_stack.pop(), third time : "<<int_stack.pop()<<"\n";

52.      

53.      cout<<"\nFloat stack\n";

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

55.      cout<<"Access using float_stack.pop(), first time :

56.                                                   "<<float_stack.pop()<<"\n";

57.      cout<<"Access using float_stack.pop(), second time:

58.                                                   "<<float_stack.pop()<<"\n";

59.      cout<<"Access using float_stack.pop(), third time :

60.                                                   "<<float_stack.pop()<<"\n";

61.      

62.      cout<<"\nString stack\n";

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

64.      do

65.      {

66.          cout<<"Access using string_stack.pop(): "<<string_stack.pop()<<"\n";

67.      }   while (!string_stack.empty());

68.    

69.    

70.     // system("pause");

71.     return  0;

72.  }

 

72 Lines: Output:

 

C++ Multi inheritance class template program example

stack<int>      int_stack;

stack<float>    float_stack;

tenouk fundamental of C++ multi-inheritance programming

 

 

 

 

 

 

 

 

 

Further C++ multi inheritance reading:

 

  1. The source code is available in C++ Multi Inheritance source code.

  2. Check the best selling C / C++ and object oriented books at Amazon.com.

  3. The Visual C++ .NET programming tutorials.

  4. See MFC library class hierarchy chart here.

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

 

 

 

 

 

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


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