본문 바로가기

Programming/C++ Basic

[Basic C++] 15 – Data Structure in C++ : Class Single Inheritance

Links to the previous posts about C++ class

2020/06/04 - [Programming/C++] - [Basic C++] 13 – Data Structure in C++ : Class 1/4

2020/06/13 - [Programming/C++] - [Basic C++] 13 – Data Structure in C++ : Class 2/4


This post is the third post of the C++ basic class series and I am going to talk about inheritance briefly in this post. Inheritance of class is one of the most important concepts when it comes to OOP (Object-Oriented Programming). This technique makes a class to derive class members from another pre-made class so that programmers can recycle the pre-made code and algorithms without writing them again. This post only shows basic knowledge about class inheritance.

 


Table of Contents

1. What is inheritance?

2. What really happens within inheritance?


1. What is inheritance (Single Inheritance)

 

The inheritance we are going to talk about this section is a single inheritance, which means that a class inherits only one class. Inheritance is a powerful technique to make your program extensible by recycling pre-made code without writing it again to produce new but similar functionality in the program.

 

You might remember how I explained OOP (Object-Oriented Programing) in one of the previous posts. The fundamental purpose of OOP is to describe what is called "abstract concepts" in the real world. It is enough to generate lines of code to imitate mathematical problems for computer languages with several mathematical data types such as int, float, and double. However, it is not possible for you to make your computer understand what human beings, animals, and plants are, and how they are categorized from general to specific.

 

A vehicle is a good example to explain the progression within a category. A vehicle is a thing used for transporting people or goods (google dictionary, 2020). It includes cars, trucks, buses, trains, and aircraft. The properties of cars are all from those of vehicles, but all vehicles are not cars.

 

There are also several kinds of cars such as sedans, trucks, and SUVs. With OOP, we can describe this real-world hierarchy in C++. Figure 1 shows the hierarchy of a vehicle class. The class inherits multiple other classes which include certain properties as a vehicle.

 

A class that inherits another class is called "Derived class" and the class from which the derived class inherited is called "Base class". 

 

Syntax of class single inheritance

class class_name : base_class_name {/*... ....*/}

The scope operator (:) and the name of a base class from which you want to inherit its class members are needed.

 

Figure 1 Hierarchy of vehicle 

 

Code example 1 shows three classes; vehicle, car, and sedan. The car class inherits from the vehicle class, and the sedan class inherits from the car class. Each class member is declared under three different access specifiers; Private, Protected, and Public.

 

Member access control in the C++ class has already been explained within the previous posting (https://nalagara.tistory.com/24). Figure 2 shows the member access control hierarchy among a base class, derived class, and other classes.

 

Table 1 Member access control

  Private Protected Public
by base class members O O O
by derived Class X O O
by non-related class X X O

 

Code example 1

class vehicle
{
private:
	int private_var;
protected:
	int protected_var;
public:
	int public_var;
};

class car : public vehicle
{
private:
	int private_var;
protected:
	int protected_var;
	vehicle::protected_var;
public:
	int public_var;
	vehicle::public_var;
};

class sedan : public car
{
private:
	int private_var;
protected:
	int protected_var;
	vehicle::protected_var;
	car::protected_var;
public:
	int public_var;
	vehicle::public_var;
	car::public_var;
};

The example is not compiled but generates errors by the compiler because variables with the same names are declared multiple times within derived classes. What you have to understand in this example is that you can use the variables that have been declared already in a base class in its derived class without declaring them again.

 

For instance, protectved_var in the car class and the same variable in the vehicle class are different because they have been declared under different namespaces. In order to use a variable has been declared in a base class, the scope resolution operator (::) is used to call the variable.


2. What really happens within inheritance - Debugging of inheritance

Now let's see what really happens when a class inherits another class using debugging. Since a class is a data type even though it is user-defined, its behavior has no difference from the ordinary data types such as int, float, and char.

 

Debugging example 1

#include <iostream>

class vehicle
{
private:
	int private_var;
protected:
	int protected_var;
public:
	int public_var;
};

class car : public vehicle
{
private:
	int private_var;
protected:
	int protected_var;
public:
	int public_var;
};

class sedan : public car
{
private:
	int private_var;
protected:
	int protected_var;
public:
	int public_var;
};

int main()
{
	std::cout << "The size of the vehicle class is :" << sizeof(vehicle) << " Bytes" << std::endl;
	std::cout << "The size of the car class is :" << sizeof(car) << " Bytes" << std::endl;
	std::cout << "The size of the sedan class is :" << sizeof(sedan) << " Bytes" << std::endl;
}

Figure 2-1 Debugging result of Vehicle class
Figure 2-2 Debugging of Car class
Figure 2-3 Debugging of Sedan class
Figure 2-4 Result of debugging example 1

 

Figure 2-1 to 2-4 show the results of the debugging of the code example above. All three classes are declared on the stack and the size of each class is 12, 24, and 36 bytes. The result indicates how inheritance works among classes.

 

The size of the vehicle class is 12 bytes, as three int variables are declared. The size of the car class is 24 bytes, as it has inherited the vehicle class, and the other three int variables are declared inside. Finally, the size of the sedan class is 36 bytes, as it has inherited two base classes directly and indirectly, and the other three int variables are declared inside. 

 

When a class inherits another class, it incorporates the class members of its base class. This is how a derived class can use variables that have not been declared inside if the variables have been already declared in its base class. 


REFERENCES

[1] http://www.cplusplus.com/doc/tutorial/inheritance/

[2] https://docs.microsoft.com/en-us/cpp/cpp/inheritance-cpp?view=vs-2019