1. Introduction
In this article, we will see about Inheritance in C++ with Simple example Program. Inheritance is code re-use mechanism. For example, we will think about truck and car. They both have the functionality of speed up, brake, starting and stopping the engine. They both have properties like fuel level, current speed, etc. But, most of the cars have 4 tires and truck may have six, eight or even 16 make you think Number of tires property may not be same for truck and car.
From the above argument of real-world items, we can put
CVehicle
as base class which holds common properties like Fuel Level and Current Speed. Similarly, the common action it can perform like accelerate and brake can be placed in
CVehicle
.
CCar
and
CTruck
can derive from the
CVehicle
. These classes have properties and behavior unique to it. Say for example both
CCar
and
CTruck
can have member variable to hold Number of tires as they vary between car and truck. This member is not an eligible property to fit in
CVehicle
.
Have a Look at below picture:

Inheritance Relation between Vehicle, Car, Truck
2. The CVehicle Class
The below code shows
CVehicle
class implementation. This class has two private members
m_FuelLevel
and
m_CurrentSpeed
. The class member functions
Accelerate()
and
Brake()
access these member variables. In these functions, we change the member variables and also display the state of the vehicle in terms of Speed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class CVehicle { protected: int m_FuelLevel; int m_CurrentSpeed; protected: void PrintVehicleProps() { cout<< "Base Properties:" << endl; cout<< "========================" << endl; cout<< "Current Speed:" << m_CurrentSpeed << endl; cout<< "Fuel Level:" << m_FuelLevel << endl; cout<< endl; } public: //Constructor CVehicle() { m_FuelLevel = 50; m_CurrentSpeed = 0; } //Increase Speed void Accelerate() { m_CurrentSpeed = m_CurrentSpeed + 15; cout<<"Action: Accelarate. Driving @"<< m_CurrentSpeed << "mph" << endl; } //Reduce the Speed by Brake void Brake() { m_CurrentSpeed = m_CurrentSpeed - 15; cout<<"Acction Brake. Driving @"<< m_CurrentSpeed << "mph" << endl; } }; |
3. Inheritance Syntax: The CCar Class
The syntax of the inheritance can be understood from the following picture:

Public Inheritance
The above picture shows that
CCar
is the new class we will create and this class will inherit common properties and behavior from existing class
CVehicle
. Remember, we created the
CVehicle
class in the previous section. In our example,
CCar
is the class ‘Derived’ from the existing ‘Base’ class
CVehicle
. The public keyword encircled here shows the mode of the inheritance. The mode of the inheritance here is public. We will look at other modes of inheritance in the later sections.
Now have a look at the code given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class CCar : public CVehicle { private: int m_trunk; public: CCar(int trunk) { m_trunk = trunk; } void PrintCarProps() { PrintVehicleProps(); cout<< "CCar Properties:" << endl; cout<< "========================" << endl; if (m_trunk > 0) cout<< "Trunk Exists" << endl; else cout<< "No Trunk" << endl; cout<< endl; } }; |
The derived class
CCar
inherits the properties and methods from the base class
CVehicle
. The
CCar
class has a new member called m_trunk which is specific to it. This new member
m_trunk
is used to specify whether a car has a storage space or not. Note that the base class properties
m_CurrentSpeed
,
m_FuelLevel
are accessed here on the derived class as they are inherited and available. In public inheritance, the derived class can access all the public and protected members of the base class.
5. The CTruck Class Inheritance
The Truck class also derived from the
CVehicle
class and uses the common properties and behaviors defined in the
CVehicle
class. But, comparing with the
CCar
it does not care about the Trunk and in place of that it deals with the property like crane type and crane length. The below code shows the
CTruck
class implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
class CTruck : public CVehicle { private: int m_CraneType; //0-Hydralic. 1-Pneumatic int m_CraneLength; public: CTruck(int type, int length) { if (m_CraneType == 0 || m_CraneType == 1) m_CraneType = type; else m_CraneType = 0; m_CraneLength = length; } void PrintTruckProps() { char type[15]; if (m_CraneType == 0) strcpy(type, "Hydralic"); else strcpy(type, "Pneumatic"); PrintVehicleProps(); cout<< "CTruck Properties:" << endl; cout<< "========================" << endl; cout<< "Crane Type" << type << endl; cout<< "Crane Length" << type << endl; cout<< endl; } }; |
Note that in both
CCar
and
CTruck
classes, the print function uses the base class version to display the properties. For Example, making a call to
PrintTruckProps()
will make a call to the base class version of the print method say
PrintVehicleProps
in our case. In the program main, we tested these inherited classes. The code snippet and the output is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
int main() { //Create Car object and display it property after //accelating the car twice CCar car(1); car.Accelerate(); car.Accelerate(); car.PrintCarProps(); //Create truck object and display properties CTruck truck(1,100); truck.PrintTruckProps(); return 0; } |

Output Of Program Example
The Complete Example code is given below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
#include "stdafx.h" #include <iostream> using namespace std; class CVehicle { protected: int m_FuelLevel; int m_CurrentSpeed; protected: void PrintVehicleProps() { cout<< "Base Properties:" << endl; cout<< "========================" << endl; cout<< "Current Speed:" << m_CurrentSpeed << endl; cout<< "Fuel Level:" << m_FuelLevel << endl; cout<< endl; } public: //Constructor CVehicle() { m_FuelLevel = 50; m_CurrentSpeed = 0; } //Increase Speed void Accelerate() { m_CurrentSpeed = m_CurrentSpeed + 15; cout<<"Action: Accelarate. Driving @"<< m_CurrentSpeed << "mph" << endl; } //Reduce the Speed by Brake void Brake() { m_CurrentSpeed = m_CurrentSpeed - 15; cout<<"Acction Brake. Driving @"<< m_CurrentSpeed << "mph" << endl; } }; class CCar : public CVehicle { private: int m_trunk; public: CCar(int trunk) { m_trunk = trunk; } void PrintCarProps() { PrintVehicleProps(); cout<< "CCar Properties:" << endl; cout<< "========================" << endl; if (m_trunk > 0) cout<< "Trunk Exists" << endl; else cout<< "No Trunk" << endl; cout<< endl; } }; class CTruck : public CVehicle { private: int m_CraneType; //0-Hydralic. 1-Pneumatic int m_CraneLength; public: CTruck(int type, int length) { if (m_CraneType == 0 || m_CraneType == 1) m_CraneType = type; else m_CraneType = 0; m_CraneLength = length; } void PrintTruckProps() { char type[15]; if (m_CraneType == 0) strcpy(type, "Hydralic"); else strcpy(type, "Pneumatic"); PrintVehicleProps(); cout<< "CTruck Properties:" << endl; cout<< "========================" << endl; cout<< "Crane Type" << type << endl; cout<< "Crane Length" << type << endl; cout<< endl; } }; int main() { //Create Car object and display it property after //accelating the car twice CCar car(1); car.Accelerate(); car.Accelerate(); car.PrintCarProps(); //Create truck object and display properties CTruck truck(1,100); truck.PrintTruckProps(); return 0; } |
6. Access Specifiers in Inheritance
We know that the access to a class’s data members and member functions are controlled by the Access Specifiers. The three access specifiers are private, public and protected. Derived class can access the protected member of the base class. Whereas, non-derived classes and global function are restricted to access these protected members. Below diagram shows the access specifiers impact over the program structure.

Access Members In Inheritance: Role of Access Specifiers
Red arrow shows that only class member functions can access the private data members and private member functions. Derived class doesn’t have access to private members of the base class. We can access public member anywhere when we have the object of the class. The point one should note here is that protected scope of a class is meant it can be accessed by inherited class but not by the outside world.
7. Private, Public, Protected Inheritance
All examples (Both car and truck inheritance) of this article shows public inheritance. In the case of public inheritance (Refer the encircled one in the picture at section 3) the members are inherited as it is. That is, in the derived class,
- Base class Public and protected members are inherited as it is.
- Private members are not inherited.
We can perform private inheritance by replacing the public as a private in the inheritance syntax. And, for protected inheritance replace public as protected.
In case of a protected inheritance, in the derived class,
- Base class public members are protected members.
- Private members not inherited.
- Protected member is inherited as it is.
So, the protected inheritance means all the inherited members as protected. In the private inheritance, in the derived class,
- Base class public and protected member are private.
- Private members are not inherited.
Even though C++ language supports protected and private inheritance, the most used inheritance is a public inheritance. If developer want to stop further inheritance, they will go for a private inheritance.
Categories: C++
Tags: Inheritance Types, Private Inheritance, Protected Inheritance, Public Inheritance