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


 

 

 

 

MODULE 15_1

C++  INHERITANCE  FEATURES 3

 

 

 

 

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.5  The Hidden Methods

  • Examine the program named inherit3.cpp carefully and you will see that it is a repeat of the inherit1.cpp with a few minor changes.

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:

 

C++ class inheritance hidden methods program output sample

class car : private vehicle
...
 
class truck : private vehicle
...
  1. The definition in the base class and

  2. The restriction on inheritance.

public:
   void initialize(int input_wheels, double input_weight, int people = 4);
   int  passengers(void)
   {return  passenger_load;}
...
int     wheels;
double  weight;
...
 
class car : private vehicle
{
     int   passenger_load;
     ...
               		}
   // 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";

15.6  Data initialization

  ...
  // trailer.initialize(18, 12500.0);
  // this method is private now
...
     // cout<<"The trailer weighs "<<trailer.get_weight()<<" kg.\n";
     // cout<<"The trailer's efficiency is "<<100.0 * trailer.efficiency()<<"%.\n";
  ...

 

  • Even though the method named efficiency() is available and can be called as a part of the truck class, it cannot be used because we have no way to initialize the wheels or weight of the truck object.

  • We can get the weight of the truck object, as we have done in line 87 as shown below,

weight = input_weight;
  • But since the weight has no way to be initialized, the result is meaningless and lines 75 and 76 are commented out.

  • The private inheritance is very similar to using an embedded object and, in fact, is rarely used. Until you gain a lot of experience with C++ and the proper use of Object Oriented Programming, you should use public inheritance exclusively.

  • There are probably not so many reasons to use private or protected inheritance. They were probably added to the language for completeness and flexibility other than security features.  Compile and run this program example.

 

15.6.1  Initializing All Data

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

 

104 Lines: Output:

 

 

 

 

 

 

 

 

 

 

 

 

 

C++ object class inheritance initialization program output example

vehicle    unicycle;

// unicycle.initialize(1, 12.5);
car      sedan_car;

tenouk C++ inheritance programming 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 2 | Main | C++ Inheritance 4 >| Site Index | Download |


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