The literal meaning of the term “inheritance” means to derive characteristics from the ancestors. Similarly, in programming, the concept of inheritance revolves around the idea of inheriting the properties of one class by its subclasses. The parent class whose properties are being inherited is known as the base class, and all the classes that inherit from the base class are the derived classes. This is one of the most important features of object-oriented programming. Inheritance enhances the efficiency of a program by providing advantages like code reusability and data hiding.
Understand the concept of inheritance with the help of a real-life example. Consider there are 3 classes: English, Physics, and Mathematics. All three of them have functions- marks(), total_students(), and faculty(). Instead of creating three separate classes with the same functions, you can create a single class Subject having these common functions, which can be inherited by these 3 classes. The following diagrammatic illustration explains this:
Without using the concept of inheritance, separate classes having common functions would cause redundancy in the program.
After using the concept of inheritance, you can use code reusability to increase the program’s efficiency and it also makes the code clean. The diagrammatic illustration above shows how you can combine three redundant classes having common features into one class whose members can be inherited by the other derived classes.
Single Inheritance in C++
The inheritance in which a single derived class is inherited from a single base class is known as the Single Inheritance. It is the simplest among all the types of inheritance since it does not include any kind of inheritance combination or different levels of inheritance. The child class can inherit all the members of the base class according to the visibility mode (i.e., private, protected, and public) that is specified during the inheritance. However, it is optional to define the visibility mode during the inheritance. If you do not define it, it sets the visibility mode to private by default.
Syntax to Implement Single Inheritance in C++
The following syntax can be used to achieve Single Inheritance in C++:
class base_class_1
{
// class definition
};
class derived_class: visibility_mode base_class_1
{
// class definition
};
Description of the Syntax:
- visibility_mode: It provides the visibility mode in the class declaration since it specifies how the members of the base class will be inherited by the derived class.
- base_class: This is the name of the class from which the derived class inherits its properties and characteristics.
- derived_class: This is the name of the derived class that can inherit the properties of the base class using the visibility mode.
Ambiguity in Single Inheritance in C++
Ambiguities can arise while implementing Single Inheritance in C++. The ambiguities happen when there are functions with the same name and return type in the base class and the derived class. By default, if an object of the derived class accesses such a function, the derived class’s function is called (which is also called function overriding). However, if you want to avoid such an ambiguous situation, then you can use a scope resolution operator to define these functions separately.
The following example illustrates ambiguity and its solution in Single Inheritance in C++.
#include <bits/stdc++.h>
using namespace std;
// base class
class organization
{
private:
string name;
string sector;
public:
organization()
{
name = "ABC";
sector = "IT";
}
// showName() function of base class
void showName()
{
cout << "The name is: " << name << endl;
cout << "The sector is: " << sector << endl;
}
};
// derived class
class employee: public organization
{
private:
string name;
int ID;
public:
employee()
{
name = "Employee_A";
ID = 1001;
}
// showName() function of derived class
void showName()
{
cout << "The name is: " << name << endl;
cout << "The ID is: " << ID << endl;
}
};
int main()
{
// create an object of the derived class
employee obj;
// call the showName() function of the base class
obj.organization::showName();
cout << "\n";
// call the showName() function of the base class
obj.employee::showName();
}
In the above program, the scope resolution operator is used to call the function showName() of the base class and the derived class having the same name and return type. Without the scope resolution operator, the function call statement will be:
obj.showName(); // showName() of derived class will be called
When the object of the derived class calls the showName() function, the showName() function of the derived class will be called, overriding the showName() of the base class. So, after modifying the above statement to the below statement:
obj.organization::showName(); // showName() of base class
// will be called
Now, it is explicitly specified that the object makes a call to the showName() function of the base class. Therefore, the base class’s showName() is called without being overridden.
Visibility Modes in Single Inheritance
The visibility mode specifies the control over the inherited members within the derived classes. A class can inherit a base class in three visibility modes in Single Inheritance:
-
Public Visibility Mode
In the public visibility mode, you can access all the members of the base class just according to their access specifiers. The members specified as public, protected, and private in the base class remain public, protected, and private respectively in the derived class as well. This means that the public members can be accessed by the derived class and all the other classes as well. Only the derived class and its members will access the protected members, while the private members will remain inaccessible by the derived class.
The following code snippet illustrates how to apply the public visibility mode in a derived class in Single Inheritance:
class base_class_1
{
// data members
// member functions
};
class derived_class: public base_class_1
{
// data members
// member functions
};
The following code displays the working of public visibility mode with all three access specifiers of the base class:
#include <bits/stdc++.h>
using namespace std;
class base_class
{
public:
int var1;
protected:
int var2;
private:
int var3;
};
// visibility mode of derived_class is public
// so access specifiers will remain the same
class derived_class : public base_class
{
};
// main function
int main()
{
derived_class obj;
// Accessible
cout << "Value of var1 is" << obj.var1 << endl;
// Not accessible
cout << "Value of var2 is" << obj.var2 << endl;
// Not accessible
cout << "Value of var3 is" << obj.var3 << endl;
};
In the above example, the visibility of the derived class derived_class is set as public so all the access specifiers will remain the same. var1 is a public member, so it is accessible outside the derived class. var2 is a protected member, so it is accessible within the derived class but not outside that, so it is not accessible in the given code. Whereas, var3 is a private member so it is not accessible outside the base class base_class.
-
Private Visibility Mode
In the private visibility mode, all the access specifiers i.e., private, protected, and the public become private. It means that you cannot access them outside of the class in which they are defined. They can only be accessed through the member functions of the same class. And since it cannot inherit the private members, these members can also be not inherited further.
The following code snippet illustrates how to apply the private visibility mode in a derived class in Single Inheritance:
class base_class_1
{
// data members
// member functions
};
class derived_class: private base_class_1
{
// data members
// member functions
};
The following code displays the working of private visibility mode with all three access specifiers of the base class:
#include <bits/stdc++.h>
using namespace std;
class base_class
{
public:
int var1;
protected:
int var2;
private:
int var3;
};
// visibility mode of derived_class is private
// so all the access specifiers will become private
class derived_class : private base_class
{
};
// main function
int main()
{
derived_class obj;
// Not Accessible
cout << "Value of var1 is" << obj.var1 << endl;
// Not accessible
cout << "Value of var2 is" << obj.var2 << endl;
// Not accessible
cout << "Value of var3 is" << obj.var3 << endl;
};
In the above example, the visibility mode of the derived class derived_class is private. So, all the members of the base class become private in the derived class. The error is thrown when the object of the derived class tries to access these members outside the class.
-
Protected Visibility Mode
In the protected visibility mode, the public and private members of the base class become protected while the private members remain private. So as a result, the public and protected members of the base class are accessed only inside the derived class. They will throw an error if you try to access them outside the derived class. Meanwhile, the private member will remain inaccessible out of the base class.
The following code snippet illustrates how to apply the protected visibility mode in a derived class in Single Inheritance:
class base_class_1
{
// data members
// member functions
};
class derived_class: protected base_class_1
{
// data members
// member functions
};
The following code displays the working of public visibility mode with all three access specifiers of the base class:
#include <bits/stdc++.h>
using namespace std;
class base_class
{
public:
int var1;
protected:
int var2;
private:
int var3;
};
// visibility mode of derived_class is protected
// so public becomes protected
class derived_class : protected base_class
{
};
// main function
int main()
{
derived_class obj;
// Not Accessible
cout << "Value of var1 is" << obj.var1 << endl;
// Not accessible
cout << "Value of var2 is" << obj.var2 << endl;
// Not accessible
cout << "Value of var3 is" << obj.var3 << endl;
};
In this example, it protects the visibility mode of the derived class derived_class. So, var1 becomes a protected member whereas var2 remains the same i.e., a protected member, and hence both are not accessible outside the derived class. However, var3 remains as a private member, so it is not accessible outside the base class base_class.
The following table illustrates the control of the derived classes over the members of the base class in different visibility modes in single inheritance :
BASE CLASS |
DERIVED CLASS |
DERIVED CLASS |
DERIVED CLASS |
Public |
Protected |
Private |
|
PUBLIC |
Public |
Protected |
Private |
PROTECTED |
Protected |
Protected |
Private |
PRIVATE |
Not Inherited / Remains Private |
Not Inherited / Remains Private |
Not Inherited / Remains Private |
Examples of Single Inheritance in C++
The following examples illustrate Single Inheritance in C++.
Example 1
The following example illustrates the working of the default constructor in Single Inheritance.
#include <iostream>
using namespace std;
// base class
class organization
{
public:
// constructor of the base class
organization()
{
cout << "I am the constructor of the base class\n\n";
}
};
// derived class
class employee: public organization
{
public:
// constructor of the derived class
employee()
{
cout << "I am the constructor of the derived class\n\n";
}
};
int main()
{
// create an object of the derived class
employee obj; // constructor of the base class
// and derived class will be called.
return 0;
}
In the above example, the base class organization is inherited by a single derived class employee. Since one class is inheriting a single base class, this is the structure of the single-level inheritance. As soon as it creates an object of the derived class, the default constructors of the base class and the derived class are called in the same order.
Example 2
The following example illustrates the working of member functions and data members in Single Inheritance in C++.
#include <iostream>
using namespace std;
// base class
class Area
{
public:
int length = 10;
int breadth = 20;
// findArea() function of base class
// to calculate the area
int findArea()
{
return length * breadth;
}
// function to print the area
void printArea()
{
cout << "The calculated area is: " << findArea() << endl;
}
};
// derived class
class Volume: public Area
{
public:
int height = 3;
// findVolume() function of derived class
// to calculate the volume
void printVolume()
{
// calculating volume using the
// result calculated in the base class.
cout << "The calculated volume is: " << (findArea() * height) << endl;
}
};
int main()
{
// create an object of the derived class
Volume obj;
// call base class function
obj. printArea();
// call derived class function
obj.printVolume();
return 0;
}
In the above example, the base class Area contains a member function findArea()which returns and prints the area. The derived class Volume accesses the findArea() function inside its printVolume() function and calculates volume using the area returned by findArea(). The object of the derived class accesses these functions of the base class and the derived class to print the results.
Final Thoughts!
To quickly sum up the tutorial, you learned an important type of inheritance called single inheritance in C++. You started with a simple introduction to inheritance and gradually moved forward with a basic explanation of single inheritance in C++. Moving ahead, you learned the syntax to implement single inheritance using the concept of parent and child classes.
Further, you saw an ambiguity in single inheritance called function overriding which happens due to the same function signature in the base as well as child classes. You saw how to use visibility modes in single inheritance to achieve the desired level of security and performance. Finally, you went through some hands-on examples of single inheritance in C++.
If you want to learn the basics of C++, you can check out Simplilearn’s complete guide/tutorial on C++ for beginners.
If you are looking for a shining career in the field of Full Stack Web Development, you can check out Simplilearn’s comprehensive training of Post Graduate Program in Full Stack Web Development. It is a 9-month self-paced program led by industry experts who are ready to guide you throughout the journey. You will learn some of the most important technologies and skills that will help you land a career in a top tech giant. Some important skills that you will learn in this course include AWS, Java, and its libraries including Hibernate, Spring, etc., DevOps, Agile, JS, HTML, CSS, Servlets, and many more.
You can also check out our complete list of Free Online Courses to upskill yourself.
If you have any queries in this article on “Single Inheritance in C++” or any other suggestions, please feel free to drop a comment below and our experts will get back to you as soon as possible.
Happy Learning!