Table of contents
1.
Introduction 
2.
Single Inheritance Block Diagram 
3.
Syntax of single inheritance in C++
4.
C++ Single Inheritance Example
5.
Ambiguity in Single Inheritance in C++
5.1.
C++
5.2.
C++
6.
Visibility Modes in Single Inheritance
6.1.
C++
6.2.
C++
6.3.
C++
7.
Frequently asked questions
8.
Key Takeaways
Last Updated: Aug 29, 2024
Easy

Single Inheritance in C++

Author Shivam Verma
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Before knowing about the single inheritance, we have to understand inheritance. Inheritance is a technique in which one class inherits the properties of another class. In other words, we can say one class access the properties of another class. A class that contains only one base class and only one derived class is called single inheritance. 

single inheritance in c++

Base Class: The class whose properties and functionalities are inherited by the derived class is called Base Class. The base class is also termed parent class or superclass. 

Derived Class: The class that extends the properties of another class is known as Derived Class. it is also known as child class or subclass.

Single Inheritance Block Diagram 

 

As shown in the above diagram, in C++ single inheritance, a single class can be derived from the base class. When we create a derived class from a base class, we use access specifiers to inherit the properties of the base class. Access specifiers can be private, protected, or public.

Syntax of single inheritance in C++

class Base_Class
{
	// class defination
};
class Derived_Class: acess_specifiers Base_Class
{
	// class defination
};
int main()
{
	Base_Class obj1;
	Derived_Class obj2;
	// code
}
You can also try this code with Online C++ Compiler
Run Code

 

Here in the above syntax, Base_Class is the name of the Base Class, Derived_Class is nothing but the name of the Derived Class, and acess_specifiers specifies how the derived class will inherit the members of the base class. access_specifiers can be private, protected, or public.

C++ Single Inheritance Example

Code 

#include <iostream>
using namespace std;
class Car
{
   	public:
   	int steering=1;
   	int wheels=4;
  
};
class Super_Car: public Car
{
   	public:
   	void high_speed()
   	{
      	cout<<" 275 km/hr.";
   	}
};
int main()
{
 	Super_Car Lamborghini;
 	cout<<"Lamborghini has "<<Lamborghini.steering<<" steering."<<endl;
 	cout<<"Lamborghini has "<<Lamborghini.wheels<<" wheels."<<endl;
 	cout<<"High speed of the Lamborghini supercar is";
 	Lamborghini.high_speed();
 	return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

Lamborghini has 1 steering.
Lamborghini has 4 wheels.
High speed of the Lamborghini supercar is 275 km/hr.


You can also compile this code with the help of Online C++ Compiler


In the above code, we have a class Car as a base class from which we have derived a subclass Super_Car. Class Super_Car inherits all the members of the Car class and can be extended to include its properties, as seen from the output.

Know about Single Inheritance in Java in detail.

Ambiguity in Single Inheritance in C++

In C++, ambiguity in single inheritance occurs when a derived class inherits from a base class, and both the base class and the derived class have member functions with the same name and signature. When an object of the derived class calls the member function, the compiler becomes confused about which version of the function to execute—the one from the base class or the one from the derived class. This ambiguity arises because the derived class does not explicitly specify which version of the function it intends to use.

For example : 

  • C++

C++

class Base {
public:
void print() {
cout << "Base class print function" << endl;
}
};

class Derived : public Base {
public:
void print() {
cout << "Derived class print function" << endl;
}
};

int main() {
Derived obj;
obj.print(); // Ambiguous: Which print() function should be called?
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

In this example, both the `Base` class and the `Derived` class have a member function named `print()`. When an object of the `Derived` class calls the `print()` function using `obj.print()`, the compiler encounters ambiguity. It is unclear whether to execute the `print()` function from the `Base` class or the `print()` function from the `Derived` class.

We can resolve this ambiguity in C++ with the help of a mechanism called "function overriding." Using the ' override ' keyword, the derived class can explicitly override the member function from the base class. This indicates to the compiler that the derived class intends to provide its implementation of the function, which should be used instead of the base class version.

Let's see how we can resolve the ambiguity in the previous example:

  • C++

C++

class Base {
public:
virtual void print() {
cout << "Base class print function" << endl;
}
};

class Derived : public Base {
public:
void print() override {
cout << "Derived class print function" << endl;
}
};

int main() {
Derived obj;
obj.print(); // Calls the print() function from the Derived class
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

By declaring the `print()` function in the `Base` class as `virtual` and using the `override` keyword in the `Derived` class, we explicitly specify that the `Derived` class is overriding the `print()` function from the `Base` class. Now, when `obj.print()` is called, the compiler knows to execute the `print()` function defined in the `Derived` class, resolving the ambiguity.

Note: It's important to remember that if the base class function is not declared as `virtual`, the derived class function will hide the base class function instead of overriding it. In such cases, the derived class function will be called when using the derived class object, but the base class function will be called when using a pointer or reference to the base class.

Visibility Modes in Single Inheritance

In C++, single inheritance allows a derived class to inherit members (data members and member functions) from a single base class. The visibility modes in single inheritance determine the accessibility of the inherited members in the derived class. C++ provides three visibility modes: public, protected, and private.

1. Public Inheritance:
When a derived class inherits from a base class using public inheritance, the public members of the base class become public members of the derived class, and the protected members of the base class become protected members of the derived class. This means that the public and protected members of the base class retain their original visibility in the derived class. The private members of the base class are not accessible directly by the derived class, but they can be accessed through public or protected member functions of the base class.

  Example:

  • C++

C++

class Base {
public:
int publicMember;
protected:
int protectedMember;
private:
int privateMember;
};

class Derived : public Base {
// publicMember is public
// protectedMember is protected
// privateMember is not accessible
};
You can also try this code with Online C++ Compiler
Run Code

 

2. Protected Inheritance:
  When a derived class inherits from a base class using protected inheritance, the public and protected members of the base class become protected members of the derived class. This means that the public and protected members of the base class are accessible by the derived class and its descendants, but they are not accessible by objects of the derived class from outside the class hierarchy. The private members of the base class are not accessible directly by the derived class.

  Example:

  • C++

C++

class Base {
public:
int publicMember;
protected:
int protectedMember;
private:
int privateMember;
};

class Derived : protected Base {
// publicMember is protected
// protectedMember is protected
// privateMember is not accessible
};
You can also try this code with Online C++ Compiler
Run Code

 

3. Private Inheritance:
  When a derived class inherits from a base class using private inheritance, the public and protected members of the base class become private members of the derived class. This means that the public and protected members of the base class are accessible only by the derived class itself and not by any other class, which includes its descendants. The private members of the base class are not accessible directly by the derived class.

  Example:

  • C++

C++

class Base {
public:
int publicMember;
protected:
int protectedMember;
private:
int privateMember;
};

class Derived : private Base {
// publicMember is private
// protectedMember is private
// privateMember is not accessible
};
You can also try this code with Online C++ Compiler
Run Code

 

It's important to choose the appropriate visibility mode based on the desired accessibility and encapsulation of the inherited members in the derived class. Public inheritance is the most common and is used when the derived class is conceptually a subtype of the base class. Protected and private inheritance is used in specific scenarios where a stronger encapsulation or implementation-only inheritance is required.

Frequently asked questions

  1. What is the main advantage of inheritance?
    Ans: The main advantage of inheritance is code reusability and readability. When a derived class inherits the properties and functionality of the base class, we need not write the same code again in the derived class.
     
  2. What is the difference between single inheritance and multiple inheritance?
    Ans: In single inheritance, the derived class inherits a single base class. Whereas in multiple inheritance, the derived class acquires two or more base classes.
     
  3. What are a base class and a derived class?
    Ans: The class whose properties and functionalities are inherited by another class is called Base Class. The base class is also termed parent class or superclass. 
    The class that extends the properties of another class is known as Derived Class. it is also known as child class or subclass.
     
  4. Can the derived class inherit static members?
    Ans: No, the derived class can not inherit static members.

Key Takeaways

In this blog, We learned what single inheritance is and how to implement it in C++.

Also check out this article - Pair in C++

Recommended Readings:

This blog is over, but If you want to take your learnings to the next level, you can use our practice platform Coding Ninjas Studio to practice various DSA questions asked in many interviews. You can also visit and read our DSA blogs by clicking here

Live masterclass