In C++, the concept of single inheritance plays a crucial role in object-oriented programming (OOP) by enabling a subclass to acquire both the attributes and functionalities of a superclass.
In C++, a superclass (base class) refers to a class that provides properties and behaviors for inheritance to promote reusability. The subclass (derived class) is the class that acquires these properties and behaviors from the base class, allowing it to leverage and apply the functionalities offered. This mechanism facilitates the derived class in accessing and making use of the features of the base class, ultimately enhancing code reusability and minimizing duplication.
Syntax
It has the following syntax:
class Base_class {
// properties and methods
};
class Derived_class : access_modifier Base_class {
// additional properties and methods
};
In this syntax:
- Base_class: It represents the class name from which the derived class inherits its properties and features.
- Derived_class: It represents the name of the class, which can inherit the properties and behavior of the base class.
- Access_modifier: It offers the access modifiers in the class declaration because it defines how the derived class will inherit the base class members.
Example of Single Inheritance in C++
Let's consider an example to demonstrate the concept of single inheritance in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
// Base class
class Person {
public:
string name;
int age;
void displayPersonInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// Derived class
class Student : public Person {
public:
string course;
void displayStudentInfo() {
displayPersonInfo(); // Access base class method
cout << "Course: " << course << endl;
}
};
int main() { //main function
Student s;
s.name = "Alice";
s.age = 20;
s.course = "Computer Science";
s.displayStudentInfo();
return 0;
}
Output:
Name: Alice
Age: 20
Course: Computer Science
Explanation
In this instance, the Student class is derived from the Person class, enabling the Student object to retrieve and exhibit personal as well as course information by utilizing inherited and custom methods.
Ambiguity in Single Inheritance in C++
In the C++ programming language, ambiguity arises in the context of single inheritance when a subclass inherits from a superclass, and both classes have member functions with identical names and parameters. In such a scenario, if an object of the derived class attempts to call a function, the compiler faces a dilemma in deciding whether to run the function from the superclass or the subclass.
This uncertainty arises when the subclass does not clearly redefine the method from the superclass or struggles to identify the correct version to invoke, resulting in possible confusion and compilation issues.
If we aim to prevent this unclear scenario, we can utilize the scope resolution operator to explicitly declare these functions individually.
Example of Ambiguity in Single Inheritance in C++
Let's consider a scenario to demonstrate the uncertainty in single inheritance in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
class Employee { //Base Class
public:
void display(int id) {
cout << "Employee Details:" << endl;
cout << "ID: " << id << endl;
cout << "Name: Alice" << endl;
}
};
class Manager : public Employee { //Derived Class
public:
// Function with same name but different parameter
void display() {
cout << "Manager Details:" << endl;
cout << "ID: 201" << endl;
cout << "Name: Michael" << endl;
cout << "Department: Web Developer" << endl;
}
};
int main() { //Main Function
Manager man;
// It calls the derived class function
man.display();
// Attempt to invoke base class version leads to a compiler error
// Correct way to call base class version
man.Employee::display(101);
return 0;
}
Output:
Manager Details:
ID: 201
Name: Michael
Department: Web Developer
Employee Details:
ID: 101
Name: Alice
Explanation
In this instance, we're examining a parent class named Employee and a subclass called Manager. Both the Employee and Manager classes contain a method named display, albeit with distinct arguments.
The subclass conceals the display(int) method inherited from the superclass employee. This situation may lead to misunderstandings that resemble ambiguity in a scenario of single inheritance. Upon invoking the man.display method, it will execute the version defined in the subclass, whereas to explicitly access the version defined in the base class, man.Employee::display(101) function should be used.
Addition Program using Single Inheritance in C++
Let's consider a program that demonstrates addition using single inheritance in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
class Input { // Base class
protected: //Access Modifier
int Num1, Num2;
public:
void getInput() {
cout << "Enter the First Number: ";
cin >> Num1;
cout << "Enter the Second Number: ";
cin >> Num2;
}
};
class Addition : public Input { //Derived Class
public:
void add() {
int sum = Num1 + Num2;
cout << "The addition of " << Num1 <<" and " << Num2 <<" is "<< sum << endl;
}
};
int main() { //Main Function
Addition obj;
obj.getInput(); // Calls base class method
obj.add(); // Calls derived class method
return 0;
}
Output:
Enter the First Number: 30
Enter the Second Number: 25
The addition of 30 and 25 is 55
Explanation
In this instance, we are examining a base class Input that receives two numerical inputs from the user. Conversely, the derived class Addition inherits these values and executes the addition operation. Subsequently, the getInput method is a member of the base class, whereas the add method within the derived class utilizes the inherited attributes to calculate and exhibit the total.
Advantages of Single Inheritance in C++
Several advantages of single inheritance in C++ are as follows:
- Code Reusability: Common attributes of a class can be defined once in the base class and reused in the derived class.
- Simpler and Cleaner Code: It helps to make code more readable and well organized as we separate common and specific features.
- Easier Maintenance: We only need to change the base class if we want to update shared functionality. It decreases bugs and simplifies debugging.
- Extensibility: It provides many ways of implementing inheritance on the existing class and can add features in the derived class while retaining the original functionality of the base class.
Limitations of Single Inheritance in C++
Several limitations of single inheritance in C++ are as follows:
- We can inherit from only one base class, not multiple classes.
- It becomes difficult to manage when the program needs features from more than one class.
- If the base class changes, it may affect the derived class also.
- Two classes that inherit from the same base class can't share code directly.
- We can't reuse features from other unrelated classes.
C++ Single Inheritance MCQs
1) What is the Single Inheritance in C++?
- A class inheriting from two classes
- A class inheriting from one class
- A class does not inherit any class
- None of the above
2) Which of the following keywords is used for inheritance in C++?
- include
- inherits
- public
- using
3) In single inheritance, what does the derived class get from the base class in C++?
- Only functions
- Both functions and variables
- Only variables
- Nothing
4) If a class B inherits from class A, which is the base class?
- Class B
- Class A
- Both Classes A and B
- None of the above
5) Which of the following options shows the correct syntax for single inheritance in C++?
- class A : B {}
- class B inherits A {}
- inherit B from A
- class B : public A {}