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


 

 

 

 

 

MODULE 15a

MORE  ON  C++  INHERITANCE  4

 

 

 

 

 

 

 

 

My Training Period: xx hours

 

This is a continuation from previous Module and a final part. The source code for this tutorial is available in C++ Inheritance source codes.

 

The C++ Inheritance programming abilities: Able to understand and use:

 

  • Method vs function.

  • Constructor Execution Order.

  • Destructor Execution Order.

  • Pointer, Array and Objects.

  • Friend functions and classes, keyword friend.

 

15.7  The Constructor Execution Order

  • The next problem we need to be concerned about is the order of constructor execution, and it is easy to remember if you remember the following statement, "C++ classes honor their base class by calling their base constructor before they call their own”.

  • In the previous program output, you can see that the base class constructor will be called before the derived class constructor. This makes sense because it guarantees that the base class is properly constructed when the constructor for the derived class is executed.

  • This allows you to use some of the data from the base class during construction of the derived class.

  • In this case, the vehicle part of the sedan_car object is constructed, and then the local portions of the sedan_car object will be constructed, so that all member variables are properly initialized. This is why we can comment out the initialize() method in line 61. It is not needed.

// sedan_car.initialize(4, 3500.0, 5);
  • When we define a trailer object in line 67, it will also be constructed in the same manner. The constructor for the base class is executed, and then the constructor for the derived class will be executed.

truck trailer;
  • The object is now fully defined and useable with default data in each member. Lines 69 and 70 are therefore not needed and commented out as shown below:

// trailer.initialize(18, 12500.0);
// trailer.init_truck(1, 33675.0);

 

15.8  The Destructor Execution Order

15.9  Inheritance And Constructors

1.          // program inherit5.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)
13.           	{
14.             		wheels = 7; weight = 11111.0;
15.             		cout<<"It is me!, Constructor #1, own by base class"<<'\n';
16.           	}
17.        	
18.          	vehicle(int input_wheels, double input_weight)
19.           	{
20.               		wheels = input_wheels; weight = input_weight;
21.               		cout<<"It is me!, Constructor #2, own by base class"<<'\n';
22.          	}
23.        	
24.          	void initialize(int input_wheels, double input_weight);
25.          	int get_wheels(void)        {return wheels;}
26.          	double get_weight(void)     {return weight;}
27.          	double wheel_load(void)     {return (weight/wheels);}
28.       };
29.        
30.       class car : public vehicle
31.       {
32.          int passenger_load;
33.          public:
34.          	car(void)
35.           	{
36.           	passenger_load = 4; cout<<"It is me!, Constructor #3, derived class, 
37.                                                                   car"<<'\n';
38.           	}
39.        	
40.          	car(int people, int input_wheels, double input_weight):vehicle(input_wheels,  
41.                                                 input_weight), passenger_load(people)
42.          	{
43.          		cout<<"It is me!, Constructor #4, derived class, car"<<'\n';
44.          	}
45.        	
46.         	void initialize(int input_wheels, double input_weight, int people = 4);
47.         	int passengers(void)        {return passenger_load;}
48.       };
49.        
50.       class truck : public vehicle
51.       {
52.          int   passenger_load;
53.          double   payload;
54.          public:
55.          	truck(void)
56.           	{
57.              		passenger_load = 3;
58.              		payload = 22222.0;
59.           	}
60.          	// the following code should be in one line.... 
61.          	truck(int people, double load, int input_wheels, double 
62.                                          input_weight):vehicle(input_wheels, 
63.                                             input_weight),passenger_load(people), 
64.                                                 payload(load)
65.              	{
66.              
67.               		cout<<"It is me!, Constructor #5, derived class, car"<<'\n';  
68.            	}
69.          	void    init_truck(int  how_many = 4, double  max_load = 24000.0);
70.          	double  efficiency(void);
71.          	int     passengers(void)        {return  passenger_load;}
72.       };
73.        
74.       // main program
75.       int  main()
76.       {
77.          	vehicle  unicycle(1, 12.5);
78.        	
79.         	// unicycle.initialize(1, 12.5);
80.         	cout<<"The unicycle has "<<unicycle.get_wheels()<<" wheel.\n";
81.         	cout<<"The unicycle's wheel load is "<<unicycle.wheel_load()<<
82.                                                       " kg on the single tire.\n";
83.         	cout<<"The unicycle weighs "<<unicycle.get_weight()<<" kg.\n\n";
84.        	
85.         	// constructor in the car class called to construct an object,
86.         	// after base class constructor called
87.         	car  sedan_car(5, 4, 3500.0);
88.        	
89.         	// constructor in the car class called to construct object
90.         	// sedan_car.initialize(4, 3500.0, 5);
91.         	cout<<"The sedan car carries "<<sedan_car.passengers()<<" passengers.\n";
92.         	cout<<"The sedan car weighs "<<sedan_car.get_weight()<<" kg.\n";
93.         	cout<<"The sedan car's wheel load is "<<sedan_car.wheel_load()<<
94.                                                              " kg per tire.\n\n";
95.        	
96.         	// constructor in the base class called to construct an object
97.         	truck  trailer(1, 33675.0, 18, 12500.0);
98.        	
99.         	// trailer.initialize(18, 12500.0);
100.     	// trailer.init_truck(1, 33675.0);
101.     	cout<<"The trailer weighs "<<trailer.get_weight()<<" kg.\n";
102.     	cout<<"The trailer's efficiency is "<<100.0 * trailer.efficiency()<<" %.\n";
103.     	
104.     	// system("pause");
105.     	return  0;
106.        }
107.        
108.   // base and derived class implementation part
109.   // initialize to any data desired
110.   void vehicle::initialize(int input_wheels, double input_weight)
111.   {
112.      wheels = input_wheels;
113.      weight = input_weight;
114.   }
115.    
116.   void car::initialize(int input_wheels, double input_weight, int people)
117.   {
118.      passenger_load = people;
119.      wheels = input_wheels;
120.      weight = input_weight;
121.   }
122.    
123.   void truck::init_truck(int how_many, double max_load)
124.   {
125.       passenger_load = how_many;
126.       payload = max_load;
127.   }
128.    
129.   double truck::efficiency(void)
130.   {
131.       return (payload / (payload + weight));
132.   }

 

132 Lines: Output:

 

C++ class inheritance constructor execution order

   ...
   vehicle(void)
   {
      wheels = 7; weight = 11111.0;
      cout<<"It is me!, Constructor #1, own by base class"<<'\n';
   }
 
   vehicle(int input_wheels, double input_weight)
   {
      wheels = input_wheels; weight = input_weight;
      cout<<"It is me!, Constructor #2, own by base class"<<'\n';
   }

       ...

vehicle    unicycle(1, 12.5);

 ...
 car(void)
 {	passenger_load = 4; cout<<"It is me!, Constructor #3, derived class, car"<<'\n';	}
 
 car(int people, int input_wheels, double input_weight):vehicle(input_wheels, input_weight), passenger_load(people)
 {	cout<<"It is me!, Constructor #4, derived class, car"<<'\n';	}

...

car(int people, int input_wheels, double input_weight):vehicle(input_wheels,
                                          input_weight), passenger_load(people)
...vehicle(input_wheels, input_weight)... 

 

  • That is exactly what it is, and it calls the constructor for the vehicle class and initializes that part of the sedan_car object that is inherited from the vehicle class. We can therefore control which base class initializer gets called when we construct an object of the derived class.

  • The next member initializer, as shown below acts like a constructor for a simple variable. By mentioning the name of the variable, passenger_load and including a value, people of the correct type within the parentheses, that value is assigned to that variable even though the variable is not a class, but a simple predefined type.

...passenger_load(people)

  • This technique can be used to initialize all members of the derived class or any portion of them. When all of the members of the member initializer list are executed, the code within the braces is executed.

  • In this case, there is no code within the executable block of the constructor. The code within the braces would be written in a normal manner for the constructor.

 

15.10  The Execution Order

truck(int people, double load, int input_wheels, double 
                                   input_weight):vehicle(input_wheels, 
                                      input_weight), passenger_load(people), 
                                          payload(load)
...
car    sedan_car(5, 4, 3500.0);
...
truck  trailer(1, 33675.0, 18, 12500.0);
...

 

15.11          Pointer, Array And Objects

1.          // program inherit6.cpp
2.          #include  <iostream>
3.           
4.          using namespace std;
5.           
6.          // base and derived class declaration part
7.          class  vehicle
8.          {
9.            protected:
10.         	int      wheels;
11.         	double   weight;
12.         public:
13.         	vehicle(void)   
14.         	{    wheels = 7; weight = 11111.0;
15.              		cout<<"Constructor #1, own by base class"<<'\n';}
16.         	vehicle(int input_wheels, double input_weight)
17.         	{    wheels = input_wheels; weight = input_weight;
18.              		cout<<"Constructor #2, own by base class"<<'\n';}
19.        	
20.         	void initialize(int input_wheels, double input_weight);
21.         	int get_wheels(void)        {return wheels;}
22.         	double get_weight(void)     {return weight;}
23.         	double wheel_load(void)     {return (weight/wheels);}
24.       };
25.        
26.       class car : public vehicle
27.       {
28.          int passenger_load;
29.          public:
30.          	car(void)
31.          	{passenger_load = 4; cout<<"Constructor #3, derived class, car"<<"\n\n";}
32.        	
33.          	car(int people, int input_wheels, double input_weight):vehicle(input_wheels,
34.                                                  input_weight),passenger_load(people)
35.           	{cout<<"Constructor #4 derived class, car"<<'\n'; }
36.        	
37.          	void initialize(int input_wheels, double input_weight, int people = 4);
38.          	int passengers(void)        {return passenger_load;}
39.       };
40.        
41.       class truck : public vehicle
42.       {
43.          int   passenger_load;
44.          double   payload;
45.          public:
46.          	truck(void)
47.          	{passenger_load = 3;
48.           	payload = 22222.0;}
49.        	
50.          	truck(int people, double load, int input_wheels, double 
51.                                           input_weight):vehicle(input_wheels, 
52.                                             input_weight),passenger_load(people), 
53.                                                   payload(load)
54.                 	{        }
55.          	void   init_truck(int  how_many = 4, double  max_load = 24000.0);
56.          	double efficiency(void);
57.          	int    passengers(void)        {return  passenger_load;}
58.       };
59.        
60.       // the main program
61.       int  main()
62.       {
63.         vehicle    unicycle;
64.        
65.         unicycle.initialize(1, 12.5);
66.        
67.         cout<<"The unicycle has " <<unicycle.get_wheels()<<" wheel.\n";
68.         cout<<"The unicycle's wheel load is "<<unicycle.wheel_load()<<
69.                                                         " kg on the single tire.\n";
70.         cout<<"The unicycle weighs "<<unicycle.get_weight()<<" kg.\n\n";
71.        
72.         car sedan_car[3];
73.         // an array of object with 3 elements
74.         int index;
75.         // variable used for counter
76.         for (index = 0 ; index < 3 ; index++)
77.         // count and execute
78.          {
79.            sedan_car[index].initialize(4, 3500.0, 5);
80.            cout<<"Count no. #" <<index<<'\n';
81.            cout<<"The sedan car carries "<<sedan_car[index].passengers()<<" 
82.                                                                       passengers.\n";
83.            cout<<"The sedan car weighs "<<sedan_car[index].get_weight()<<" kg.\n";
84.            cout<<"The sedan car's wheel load is "<<sedan_car[index].wheel_load()<<
85.                                                                 " kg per tire.\n\n";
86.          }
87.        
88.         truck  *trailer;   // a pointer
89.        
90.         trailer = new truck;   
91.         // initialize to point to something...point to an object
92.        
93.          if (trailer == NULL)
94.            {
95.                cout<<"Memory allocation failed\n";
96.                exit(EXIT_FAILURE);
97.            }
98.             trailer->initialize(18, 12500.0);
99.             trailer->init_truck(1, 33675.0);
100.     cout<<"The trailer weighs " << trailer->get_weight()<<" kg.\n";
101.     cout<<"The trailer's efficiency is "<<100.0 * trailer->efficiency()<<
102.                                          " %.\n";
103.    
104.     delete trailer;
105.     // deallocate the object
106.    
107.    
108.    // system("pause");
109.    return  0;
110.    }
111.    
112.   // base and derived class implementation part
113.   // initialize to any data desired
114.   void vehicle::initialize(int input_wheels, double input_weight)
115.   {
116.      wheels = input_wheels;
117.      weight = input_weight;
118.   }
119.    
120.   void car::initialize(int input_wheels, double input_weight, int people)
121.   {
122.       passenger_load = people;
123.       wheels = input_wheels;
124.       weight = input_weight;
125.   }
126.    
127.   void truck::init_truck(int how_many, double max_load)
128.   {
129.       passenger_load = how_many;
130.       payload = max_load;
131.   }
132.    
133.   double truck::efficiency(void)
134.   {
135.       return (payload / (payload + weight));
136.   }

 

136 Lines: Output:

 

 

 

 

 

 

 

 

 

 

 

C++ class inheritance object, pointer and array program example

car  sedan_car[3];
sedan_car[index].initialize(4, 3500.0, 5);
cout<<"Count no. #" <<index<<'\n';
cout<<"The sedan car carries "<<sedan_car[index].passengers()<<" passengers.\n";
cout<<"The sedan car weighs "<<sedan_car[index].get_weight()<<" kg.\n";
cout<<"The sedan car's wheel load is "<<sedan_car[index].wheel_load()<<" kg per tire.\n\n";
  truck  *trailer;   // a pointer
 
  trailer = new truck;   
  // initialize to point to something...point to an object
trailer->initialize(18, 12500.0);
trailer->init_truck(1, 33675.0);
cout<<"The trailer weighs " << trailer->get_weight()<<" kg.\n";
cout<<"The trailer's efficiency is "<<100.0 * trailer->efficiency()<<" %.\n";
delete   trailer;
// de-allocate the object from memory

tenouk C++ object inheritance tutorial

 

 

 

 

 

 

 

 

 

 

 

 

Further C++ Inheritance reading:

 

  1. This is a continuation from previous Module. 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 3 | Main | C++ Inheritance 5 >| Site Index | Download |


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