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


 

 

 

 

 

MODULE 15

C++  INHERITANCE  PROGRAMMING 2

 

 

 

 

 

My Training Period: xx hours

The source code for this tutorial is available in C++ Inheritance source codes.

The C++ programming abilities that supposed to be acquired: Able to understand and use:

 

 

 

 

 

15.1  An Introduction

  • This Module will illustrate some features of the finer points of class inheritance and what it can be used for.

  • Examine the following inherit1.cpp program example.  It is identical to the program developed in Module 14 named allvehicle.cpp except the program code is rearranged.

1.     // program inherit1.cpp

2.     #include <iostream>

3.     using namespace std;

4.      

5.     // class declaration part

6.     class vehicle

7.     {

8.         // protected keyword: these variables will be automatically

9.         // inherited by all the derived class but not outside the

10.       // base and derived class of vehicle

11.       protected:

12.              int    wheels;

13.              double weight;

14.       public:

15.              void  initialize(int input_wheels, double input_weight);

16.              int  get_wheels(void)        {return wheels;}

17.              double  get_weight(void)     {return weight;}

18.              double  wheel_load(void)  {return (weight/wheels);}

19.    };

20.   

21.    // derived class declaration part

22.  class car : public vehicle

23.  {

24.         int passenger_load;

25.         public:

26.              void initialize(int input_wheels, double input_weight, int people = 4);

27.              int passengers(void)

28.              {

29.                   return passenger_load;

30.              }

31.  };

32.   

33.  class truck : public vehicle

34.  {

35.          int     passenger_load;

36.          double  payload;

37.          public:

38.               void    init_truck(int  how_many = 4, double  max_load = 24000.0);

39.               double  efficiency(void);

40.               int     passengers(void)        {return  passenger_load;}

41.  };

42.   

43.  // the main program

44.  int main()

45.  {

46.     vehicle  unicycle;

47.     unicycle.initialize(1, 12.5);

48.   

49.     cout<<"Using base class, vehicle\n";

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

51.     cout<<"The unicycle has " <<unicycle.get_wheels()<<" wheel.\n";

52.     cout<<"The unicycle's wheel loading is "<<unicycle.wheel_load()<<" kg on the

53.                                                               single tire.\n";

54.     cout<<"The unicycle weighs "<<unicycle.get_weight()<<" kg.\n\n";

55.   

56.     car  sedan_car;

57.     sedan_car.initialize(4, 3500.0, 5);

58.   

59.     cout<<"Using derived class, car\n";

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

61.     cout<<"The sedan car carries "<<sedan_car.passengers()<<" passengers.\n";

62.     cout<<"The sedan car weighs "<<sedan_car.get_weight()<<" kg.\n";

63.     cout<<"The sedan's car wheel loading is "<<sedan_car.wheel_load()<<" kg per

64.                                                                     tire.\n\n";

65.     truck trailer;

66.     trailer.initialize(18, 12500.0);

67.     trailer.init_truck(1, 33675.0);

68.   

69.     cout<<"Using derived class, truck\n";

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

71.     cout<<"The trailer weighs "<< trailer.get_weight()<< " kg.\n";

72.     cout<<"The trailer's efficiency is "<<100.0 * trailer.efficiency()<<" %.\n";

73.    

74.     // system("pause");

75.     return 0;

76.  }

77.   

78.  // base and derived class implementation part

79.  // initialize to any data desired, own by base class

80.  void vehicle::initialize(int input_wheels, double input_weight)

81.  {

82.     wheels = input_wheels;

83.     weight = input_weight;

84.  }

85.   

86.  // initialize() method own by derived car class

87.  void car::initialize(int input_wheels, double input_weight, int people)

88.  {

89.      // class base variables used by derived car class,

90.      // because of the protected keyword

91.      passenger_load = people;

92.      wheels = input_wheels;

93.      weight = input_weight;

94.  }

95.   

96.  void truck::init_truck(int how_many, double max_load)

97.  {

98.       passenger_load = how_many;

99.       payload = max_load;

100. }

102.      

103. double truck::efficiency(void)

104. {

105.        return payload / (payload + weight);

106. }

 

106 Lines: Output:

 

 

 

 

 

 

 

 

 

 

 

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

 

C++ class object inheritance program output example

15.2       The Scope Operator

void    vehicle::initialize(int    input_wheels, double    input_weight)

15.3    A Protected Data

...

protected:

       int    wheels;

       double weight;

...

void car::initialize(int input_wheels, double input_weight, int people)

{

    // class base variables used by derived car class,

    // because of the protected keyword

    passenger_load = people;

    wheels = input_wheels;

    weight = input_weight;

}

Specifier

Description

private

The variables and methods are not available to any outside calling routines, and they also are not available to any derived classes inheriting this class.  Class members are private by default.

You can override the default struct access with private or protected but you cannot override the default union access. friend declarations are not affected by this access specifier.

protected

The variables and methods are not available to any outside calling routines, but they are directly available to any derived class inheriting this class.  You can override the default struct access with private or protected but you cannot override the default union access.  friend declarations are not affected by this access specifier.

public

All variables and methods are freely available to all outside calling routines and to all derived classes.  Members of a struct or union are public by default.

You can override the default struct access with private or protected but you cannot override the default union access. friend declarations are not affected by this access specifier.

 

Table 15.1:  Member variable and method specifier

 

  • These keywords when used are effective until one of the other keyword is found in a sequence manner.  So, depend on your need, they can be reused in the same block of codes.

  • These three definitions can also be used in a struct data type. The difference with a struct is that everything defaults to public until one of the other keywords is used.

  • Compile and run this program before continuing on the next program example.

15.4   The Private Data And Inheritance

  • Examine the program named inherit2.cpp where the data in the base class is permitted to use the default private because line 8 is commented out.

 

1.            // program inherit2.cpp
2.            #include  <iostream>
3.            using namespace std;
4.            
5.            // a base and derived class declaration part
6.            class vehicle
7.            {
8.                  // protected:
9.                  // note this is removed, so it is private now
10.                int     wheels;
11.                double  weight;
12.                public:         // a public specifier
13.                void    initialize(int input_wheels, double input_weight);
14.                int     get_wheels(void)        {return wheels;}
15.                double  get_weight(void)     {return weight;}
16.                double  wheel_load(void)  {return (weight/wheels);}
17.            };
18.               
19.           class car : public vehicle
20.           {
21.                 int   passenger_load;
22.                 public:
23.                 void  initialize(int input_wheels, double input_weight, int people = 4);
24.                 int    passengers(void)        {return passenger_load;}
25.            };
26.            
27.            class truck : public vehicle
28.            {
29.                 int  passenger_load;
30.                 double  payload;
31.                 public:
32.                 void  init_truck(int how_many = 4, double max_load = 24000.0);
33.                 double efficiency(void);
34.                 int passengers(void)        {return  passenger_load;}
35.            };
36.            
37.            // the main program
38.            int main()
39.            {
40.                 vehicle unicycle;
41.                 unicycle.initialize(1, 12.5);
42.               
43.                 cout<<"Using base class, vehicle\n";
44.                 cout<<"-------------------------\n";
45.                 cout<<"The unicycle has "<<unicycle.get_wheels()<<" wheel.\n";
46.                 cout<<"The unicycle's wheel load is "<<unicycle.wheel_load()<<" kg 
47.                                                                  on the single tire.\n";
48.                 cout<<"The unicycle weighs "<<unicycle.get_weight()<<" kg.\n\n";
49.               
50.                 car sedan_car;
51.                 sedan_car.initialize(4, 3500.0, 5);
52.               
53.                 cout<<"Using derived class, car\n";
54.                 cout<<"------------------------\n";
55.                 cout<<"The sedan car carries "<<sedan_car.passengers()<<" passengers.\n";
56.                 cout<<"The sedan car weighs "<<sedan_car.get_weight()<< " kg.\n";
57.                 cout<<"The sedan car's wheel loading is "<<sedan_car.wheel_load()<<
58.                                                                      " kg per tire.\n\n";
59.               
60.                 truck trailer;
61.                 trailer.initialize(18, 12500.0);
62.                 trailer.init_truck(1, 33675.0);
63.               
64.                 cout<<"Using derived class, truck\n";
65.                 cout<<"--------------------------\n";
66.                 cout<<"The trailer weighs "<<trailer.get_weight()<<" kg.\n";
67.                 cout<<"The trailer's efficiency is "<<100.0 * trailer.efficiency()<<" %.\n";
68.                 
69.                 // system("pause");
70.                 return 0;
71.            }
72.            
73.            // base and derived class implementation part
74.            // initialize to any data desired, this method own by base class
75.            void vehicle::initialize(int input_wheels, double input_weight)
76.            {
77.                   wheels = input_wheels;
78.                   weight = input_weight;
79.            }
80.               
81.            // this method own by derived class
82.            void car::initialize(int input_wheels, double input_weight, int people)
83.            {
84.                 passenger_load = people;
85.               
86.                // this variable are invalid anymore because both wheels
87.                // and weight are private now.
88.                // wheels = input_wheels;
89.                // weight = input_weight;
90.                vehicle::initialize(input_wheels, input_weight);
91.                // added statement, using base class method instead of derived class
92.            }
93.               
94.            void truck::init_truck(int how_many, double max_load)
95.            {
96.                 passenger_load = how_many;
97.                 payload = max_load;
98.            }
99.            
100.          double truck::efficiency(void)
101.          {
102.              // changed from program inherit1.cpp, from weight to get_weight()
103.              return payload / (payload + get_weight());
104.          }

 

104 Lines: Output:

 

C++ class inheritance protected, private and public keywords illustration

   // wheels = input_wheels;
// weight = input_weight;
vehicle::initialize(input_wheels, input_weight);
// added statement, using base class method instead of derived class
wheels = input_wheels;
weight = input_weight;

 

tenouk C++ inheritance programming object oriented tutorial

 

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ reading and digging:

 

  1. The source code for this tutorial is available in C++ Inheritance source codes.
  2. The 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.

 

 

 

 

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


C++ Inheritance Features:  Part 1 | Part 2 | Part 3 | Part 4