C++ Single Inheritance

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 with name and age attributes and a constructor to initialize them.
  • The displayPerson method in the Person class prints the details of the person.
  • The Student class inherits from the Person class using the public keyword. This means all public members of Person become public members of Student.
  • The Student class adds an additional attribute, grade, and a constructor to initialize all attributes, including those inherited from the Person class.
  • The displayStudent method in the Student class calls the displayPerson method from the Person class to print the name and age, then prints the grade.
  • An object of the Student class is created in the main function, and its details are displayed using the displayStudent method.

Advantages of Single Inheritance

  1. Code Reusability: The derived class reuses the attributes and methods of the base class, reducing code duplication.
  2. Simplicity: Single inheritance is simpler to understand and manage compared to multiple inheritance.
  3. 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top