Write a Program to Implement Single Inheritance with Base and Derived Classes

Write a Program to Implement Single Inheritance with Base and Derived Classes

👨‍💻 Understanding Single Inheritance in C++
Inheritance is one of the key pillars of object-oriented programming (OOP) in C++. It allows one class (called the derived class) to inherit properties and behaviors (i.e., data members and member functions) from another class (called the base class). This promotes code reuse, extensibility, and hierarchical classification.

📌 What is Single Inheritance?
Single inheritance is the simplest form of inheritance where a class inherits from only one base class. It provides a foundation for understanding more complex inheritance structures like multiple or multilevel inheritance.

✅ Key Benefits of Single Inheritance
  • Code Reusability: Common code is written once in the base class and reused in derived classes.
  • Logical Hierarchy: Reflects real-world relationships (e.g., Animal → Dog).
  • Maintainability: Easy to maintain and extend code.
🧑‍💻 C++ Program: Demonstrating Single Inheritance
Let’s write a program to demonstrate how single inheritance works using a simple example of a base class `Person` and a derived class `Student`.
#include <iostream>
using namespace std;
// Base Class
class Person {
protected:
    string name;
    int age;
public:
    void getDetails(string n, int a) {
        name = n;
        age = a;
    }
    void showDetails() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
    }
};
// Derived Class
class Student : public Person {
private:
    int rollNo;
    string course;
public:
    void setStudentData(int r, string c) {
        rollNo = r;
        course = c;
    }
    void showStudentData() {
        showDetails(); // Calling base class function
        cout << "Roll No: " << rollNo << endl;
        cout << "Course: " << course << endl;
    }
};
int main() {
    Student s1;
    s1.getDetails("Vivek", 21);     // Base class function
    s1.setStudentData(1001, "BCA"); // Derived class function
    cout << "\nStudent Information:\n";
    s1.showStudentData();
    return 0;
}

🖨️ Output
Student Information:
Name: Vivek
Age: 21
Roll No: 1001
Course: BCA

🧠 Code Breakdown
  • `Person` is the base class with attributes `name` and `age`, and methods to get and show these details.
  • `Student` is the derived class that inherits from `Person` using `public` access specifier.
  • `Student` adds its own data members: `rollNo` and `course`, and has methods to set and display them.
  • The function `showStudentData()` calls the base class method `showDetails()` to demonstrate inheritance.
🛠️ Notes on Access Specifiers
  • `protected` members of the base class are accessible in the derived class.
  • `public` inheritance makes base class's `public` members remain `public` in the derived class.
📝 Conclusion
Single inheritance is a foundational concept in C++ object-oriented programming. It allows derived classes to reuse and extend the functionality of base classes efficiently. This reduces redundancy and promotes organized code architecture.

Comments