Introduction
Single inheritance in C++ allows a derived class to inherit members (attributes and methods) from a single base class. This type of inheritance promotes code reusability and establishes a relationship between the base class and the derived class.
Example: Implementing Single Inheritance
Let’s explore a simple example of single inheritance where a derived class inherits from a base class.
Base Class: Person
The Person
class will contain basic information about a person.
Derived Class: Student
The Student
class will inherit from the Person
class and add additional attributes specific to a student.
Code Example
#include <iostream>
using namespace std;
// Base class
class Person {
public:
string name;
int age;
// Constructor to initialize name and age
Person(string n, int a) : name(n), age(a) {}
// Method to display person details
void displayPerson() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Derived class inheriting from Person
class Student : public Person {
public:
double grade;
// Constructor to initialize name, age, and grade
// Calls the constructor of the base class
Student(string n, int a, double g) : Person(n, a), grade(g) {}
// Method to display student details
// Calls the method from the base class and adds additional information
void displayStudent() {
displayPerson(); // Call base class method
cout << "Grade: " << grade << endl;
}
};
int main() {
// Create an object of the derived class
Student student("Aarav", 20, 88.5);
// Display student details
student.displayStudent();
return 0;
}
Output
Name: Aarav, Age: 20
Grade: 88.5
Explanation
- The
Person
class is defined withname
andage
attributes and a constructor to initialize them. - The
displayPerson
method in thePerson
class prints the details of the person. - The
Student
class inherits from thePerson
class using thepublic
keyword. This means all public members ofPerson
become public members ofStudent
. - The
Student
class adds an additional attribute,grade
, and a constructor to initialize all attributes, including those inherited from thePerson
class. - The
displayStudent
method in theStudent
class calls thedisplayPerson
method from thePerson
class to print the name and age, then prints the grade. - An object of the
Student
class is created in themain
function, and its details are displayed using thedisplayStudent
method.
Advantages of Single Inheritance
- Code Reusability: The derived class reuses the attributes and methods of the base class, reducing code duplication.
- Simplicity: Single inheritance is simpler to understand and manage compared to multiple inheritance.
- Encapsulation: Single inheritance promotes encapsulation by allowing derived classes to extend the functionality of base classes without modifying them.
Conclusion
Single inheritance in C++ is a fundamental concept that allows a derived class to inherit members from a single base class. This example demonstrated how to define a base class and a derived class, initialize their members, and use their methods. Understanding single inheritance is crucial for writing modular, reusable, and maintainable C++ code.