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

You will notice that the derived classes named car and truck have the keyword private instead of the public prior to the name of the base class in the first line of each class declaration as shown below:
class car : private vehicle ... class truck : private vehicle ... The keyword public, when included prior to the base class name, makes all of the methods defined in the base class available for use in the derived class at the same security level as they were defined in the base class.
Therefore, in the previous program, we were permitted to call the methods defined as part of the base class from the main program even though we were working with an object of the derived classes.
In this program, all entities are inherited as private due to the use of the keyword private prior to the name of the base class. They are therefore unavailable to any code outside of the derived class.
The general rule is that all elements are inherited into the derived class at the most restrictive of the two restrictions placed on them,
The definition in the base class and
The restriction on inheritance.
This defines the way the elements are viewed outside of the derived class.
The elements are all inherited into the derived class such that they have the same level of protection they had in the base class, as far as their visibility restrictions within the derived class.
We have returned to use the protected instead of private in the base class; therefore the member variables are available for use within the derived class only.
In the this program, the only methods available for objects of the car class, are those that are defined as part of the class itself, and therefore we only have the methods named initialize() and passengers() available for use with objects of car class as shown below:
public: void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} When we declare an object of type car, it contains three variables. It contains the one defined as part of its class named passenger_load and the two that are part of its base class, wheels and weight as shown below:
... int wheels; double weight; ... class car : private vehicle { int passenger_load; ... } All are available for direct use within its methods because of the use of the keyword protected in the base class. The variables are part of an object of class car when it is declared and are stored as part of the object.
You will notice that several of the output statements have been commented out in the main program since they are no longer legal operations.
Lines 60 through 62 have been commented out as shown below, because the methods named get_weight() and wheel_load() are not available as members of the car class because we are using private inheritance.
// cout<<"The sedan car weighs "<<sedan_car.get_weight()<<" kg.\n";
// cout<<"The sedan car's wheel loading is "<<sedan_car.wheel_load()<<" kg per // tire.\n\n"; You will notice that initialize() is still available but this is own by the car class, not the similar method of the same name in the vehicle class (base class).
Moving on to the use of the truck class in the main program, we find that lines 65 and 66 are commented out as shown below, for the same reason as given above,
...
// trailer.initialize(18, 12500.0); // this method is private now ... But lines 75 and 76 as shown below are commented out for an entirely different reason.
// cout<<"The trailer weighs "<<trailer.get_weight()<<" kg.\n"; // cout<<"The trailer's efficiency is "<<100.0 * trailer.efficiency()<<"%.\n"; ...
weight = input_weight;
|
15.6.1 Initializing All Data
Examine the program example named inherit4.cpp, you will find that we have fixed the initialization problem that we left dangling in the last program example.
1. // program inherit4.cpp
2. #include <iostream>
3. using namespace std;
4.
5. // base and derived class declaration part 6. class vehicle
7. {
8. protected:
9. int wheels;
10. double weight;
11. public:
12. vehicle(void) {wheels = 7; weight = 11111.0;
13. cout<<"Constructor's value of the base class, vehicle"<<'\n';
14. cout<<"----------------------------------------------\n";}
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. // public inheritance 22. class car : public vehicle //public inheritance
23. {
24. int passenger_load;
25. public:
26. car(void) {passenger_load = 4;
27. cout<<"Constructor's value of the derived class, car"<<'\n';
28. cout<<"---------------------------------------------\n";}
29. void initialize(int input_wheels, double input_weight, int people = 4);
30. int passengers(void) {return passenger_load;}
31. };
32.
33. class truck : public vehicle // public inheritance
34. {
35. int passenger_load;
36. double payload;
37. public:
38. truck(void) {passenger_load = 3;payload = 22222.0;
39. cout<<"Constructor's value of the derived class, truck"<<'\n';
40. cout<<"-----------------------------------------------\n";}
41. void init_truck(int how_many = 4, double max_load = 24000.0);
42. double efficiency(void);
43. int passengers(void) {return passenger_load;}
44. };
45.
46. // the main program 47. int main()
48. {
49. vehicle unicycle;
50.
51. // unicycle.initialize(1, 12.5);
52. // use default constructor value, so no need the
53. // initialization code for object unicycle anymore.
54. cout<<"The unicycle has "<<unicycle.get_wheels()<<" wheel.\n";
55. cout<<"The unicycle's wheel loading is "<<unicycle.wheel_load()<<" kg
56. on the single tire.\n";
57. cout<<"The unicycle weighs "<<unicycle.get_weight()<<" kg.\n\n";
58.
59. car sedan_car;
60. // use base class initialize() method
61. // sedan_car.initialize(4, 3500.0, 5);
62. cout<<"The sedan car carries "<<sedan_car.passengers()<<" passengers.\n";
63. cout<<"The sedan car weighs "<<sedan_car.get_weight()<<" kg.\n";
64. cout<<"The sedan car's wheel loading is "<<sedan_car.wheel_load() <<
65. " kg per tire.\n\n";
66.
67. truck trailer;
68. // use base class initialize() method with default data
69. // trailer.initialize(18, 12500.0);
70. // trailer.init_truck(1, 33675.0);
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. // class implementation part 79. // initialize to any data desired
80. void vehicle::initialize(int input_wheels, double input_weight)
81. // base class's method
82. {
83. wheels = input_wheels;
84. weight = input_weight;
85. }
86.
87. void car::initialize(int input_wheels, double input_weight, int people)
88. // derived class's method
89. {
90. passenger_load = people;
91. wheels = input_wheels;
92. weight = input_weight;
93. }
94.
95. void truck::init_truck(int how_many, double max_load)
96. {
97. passenger_load = how_many;
98. payload = max_load;
99. }
100.
101. double truck::efficiency(void)
102. {
103. return (payload / (payload + weight));
104. }

We also added default constructors to each of the classes so we can study how they are used when we use inheritance; and we have returned to the use of public inheritance.
When we create an object of the base class vehicle, there is no problem since inheritance is not a factor. The constructor for the base class operates in exactly the same as all the constructors have in previous Module.
You will notice that we create the unicycle object in line 49 as shown below, using the default constructor and the object is initialized to the values contained in the constructor.
vehicle unicycle;
Line 51 is commented out because we no longer need the initialization code for the object.
// unicycle.initialize(1, 12.5); When we define an object of the derived classes in line 59, as shown below, it is a little different because not only do we need to call a constructor for the derived class; we have to worry about how we get the base class initialized through its constructor also.
car sedan_car; Actually, it is not a problem because the compiler will automatically call the default constructor for the base class unless the derived class explicitly calls another constructor for the base class.
We will explicitly call another constructor in the next program example, but for now we will only be concerned about the default constructor for the base class that is called automatically.
tenouk C++ inheritance programming tutorial