|
My Training Period: yy hours.
The source code for this tutorial is available in C++ Inheritance source codes.
The C++ programming abilities that supposed to be acquired:
10.1 Introduction To Inheritance
14.2 The Starting Point
1. // a class declaration part, vehicle.h, header file 2. // save and include this file in your next project 3. // do not compile or run 4. 5. #ifndef VEHICLE_H //preprocessor directive 6. #define VEHICLE_H 7. 8. // class declaration part – the interface 9. class Cvehicle 10. { 11. protected: 12. int wheels; 13. int weight; 14. public: 15. void initialize(int input_wheels, float input_weight); 16. int get_wheels(void); 17. float get_weight(void); 18. float wheel_load (void); 19. }; 20. #endif
20 Lines of codes
Program 14.1: vehicle.h program, declaration of Cvehicleclass |
We will eventually refer to this as a base class or parent class, but for the time being, we will simply use it like any other class to show that it is indeed just a normal class.
Note that we will explain the added keyword protected shortly. Figure 14.1 is a graphical representation of the Cvehicle class. Ignore lines 5, 6, and 11, they will be explained in detail later. This program cannot be compiled or executed because it is only a header file without the main() but makes sure there are no error such as typing errors.
----------------------------------------------------------------------------
Next, examine the file named vehicle.cpp, and you will find that it is the implementation of the vehicle class. Theinitialize() method assigns the values input as parameters to the wheels andweight variables.
1. // program vehicle.cpp, implementation part,
2. // compile without error, generating object file, do not run
3. #include "vehicle.h"
4.
5. // class implementation part
6. // initialize to data input by user
7. void Cvehicle::initialize(int input_wheels, float input_weight)
8. {
9. wheels = input_wheels;
10. weight = input_weight;
11. }
12.
13. // get the number of wheels of this vehicle
14. int Cvehicle::get_wheels()
15. {
16. return wheels;
17. }
18.
19. // return the weight of this vehicle
20. float Cvehicle::get_weight()
21. {
22. return weight;
23. }
24.
25. // return the load on each wheel
26. float Cvehicle::wheel_load()
27. {
28. return (weight/wheels);
29. }
Program 14.2: vehicle.cpp, implementation program of Cvehicle class
We have methods to return the number of wheels and theweight, and finally, we have one that does a calculation to return the load on each wheel. At this point, we are more interested in learning how to implement the interface to the classes.
We will use it as a base class later. Compile this program without error, generating object file, in preparation for the next program example, but you cannot execute it because there is no main() entry point.
The file namedtransprt.cpp uses the Cvehicle class in similar manner as we have illustrated in the previous Module. This should be an indication to you that the Cvehicle class is just a normal class as defined in C++.
1. // program transprt.cpp, the main program,
2. // compile and run
3.
4. #include <iostream>
5. using namespace std;
6. #include "vehicle.h"
7. // a user define header file and put it in the same folder as this program
8.
9. void main()
10. {
11. Cvehicle car, motorcycle, truck, sedan_car;
12. // 4 objects instantiated
13.
14. // data initialization
15. car.initialize(4,3000.0);
16. truck.initialize(20,30000.0);
17. motorcycle.initialize(2,900.0);
18. sedan_car.initialize(4,3000.0);
19.
20. // display the data
21. cout<<"The car has "<<car.get_wheels()<< " tires.\n";
22. cout<<"Truck has load "<<truck.wheel_load()<<" kg per tire.\n";
23. cout<<"Motorcycle weight is "<<motorcycle.get_weight()<<" kg.\n";
24. cout<<"Weight of sedan car is "<<sedan_car.get_weight()<<" kg, and has
25. "<<sedan_car.get_wheels()<<" tires.\n";
26.
27. // system("pause");
28. }
Program 14.3: transprt.cpp the main program
Cvehicle car, motorcycle, truck, sedan_car; // 4 objects instantiated
// data initialization car.initialize(4, 3000.0); truck.initialize(20, 30000.0); motorcycle.initialize(2,900.0); sedan_car.initialize(4, 3000.0);
|
Examine the following file named car.h, for our first example of using a derived class. The Cvehicle class is inherited due to the ":public Cvehicle" code added to line 12 as shown below:
class Ccar : public Cvehicle
1. // another class declaration car.h
2. // save and include this file in your project
3. // do not compile or run.
4.
5. #ifndef CAR_H // to avoid car.h re-inclusion/multi inclusion
6. #define CAR_H
7.
8. #include "vehicle.h"
9.
10. // a derived class declaration part
11. // Ccar class derived from Cvehicle class
12. class Ccar : public Cvehicle
13. {
14. int passenger_load;
15. public:
16. // this method will be used instead of the same
17. // method in Cvehicle class - overriding
18. void initialize(int input_wheels, float input_weight, int people = 4);
19. int passengers(void);
20. };
21.
22. #endif
Program 14.4: car.h program, declaration of derived class Ccar
This derived class named Ccar is composed of all the information included in the base class Cvehicle, and all of its own additional information. Even though we did nothing to the class named Cvehicle, we made it available in this car.h program.
In fact, it can be used as a normal class and a base class in the same program.
A class that inherits another class is called a derived class or a child class.
Likewise the terminology for the inherited class is called a base class, butparent class and super class are sometimes used.
A base class is rather general class which can cover a wide range of objects attributes or behavior or properties, whereas a derived class is somewhat more specific but at the same time more useful.
For simple example consider your family. You and your siblings inherited many characteristics of your mother and/or father such as eye and hair colors. Your father and/or mother are base classes, whereas you and your siblings are derived classes.
In this case, the Cvehicle base class can be used to declare objects that represent plane, trucks, cars, bicycles, or any other vehicles you can think up.
The class namedCcar however can only be used to declare an object that is of type Ccar because we have limited kinds of data that can be intelligently used with it. The car class is therefore more restrictive and specific than theCvehicle class.
If we wish to get even more specific, we could define a derived class usingCcar as the base class, name itCsports_car, and include information such asred_line_limit for the tachometer orturbo_type_engine as new member variables.
Then, theCcar class would therefore be used as a derived class and a base class at the same time, so it should be clear that these names refer to how the class is used.
A derived class is defined by including the header file of the base class as is done in line 8 as shown below:
#include "vehicle.h"
And then the name of the base class is given following the name of the derived class separated by a colon ( : ) as is illustrated in line 12 as shown below:
class Ccar : public Cvehicle
Ignore the keywordpublic immediately following the colon in this line. This defines public inheritance and we will discuss it later.
All objects declared as being of class Ccar therefore are composed of the two variables from the Cvehicle class because they inherit those variables, and the single member variable declared in the Ccar class namedpassenger_load, and the total member variable is listed below:
▪ int wheels;
▪ int weight;
▪ int passenger_load;
An object of this class also will have three of the four methods ofCvehicle as listed below:
▪ get_wheels( ) { }
▪ get_weight( ) { }
▪ wheel_load( ) { }
And the two new methods:
▪ initialize( ) { }
▪ passengers( ) { }
The method namedinitialize() which is part of theCvehicle class will not be available here because it is hidden by the local version of initialize() which is a part of the Ccar class. The local method will be used if the name is similar, allowing you to customize your new class. Figure 14.2 is a graphical representation of an object of this class.
Note once again that the implementation for the base class only needs to be supplied in its compiled form of the vehicle.cpp file. The source code for implementation can be hidden.
The header file for the base class, vehicle.h, must be available as a text file since the class definitions are required in order to use the class.
The following figure is an illustration of our simple class hierarchy. Here, car inherits some of the common vehicle’s properties.
tenouk C++ on object oriented: Inheritance principles