Single Inheritance in C++


Single Inheritance in C++

Inheritance is a key feature of object-oriented programming (OOP), allowing one class to inherit properties and behaviors (i.e., methods and variables) from another class. In single inheritance, one class inherits from only one base class.

1️⃣ What is Single Inheritance?

In single inheritance, a class (derived class) inherits from a single base class. This allows the derived class to inherit the properties and methods of the base class, while also being able to extend or modify them.

Key Points of Single Inheritance:

  • One Base Class and One Derived Class.

  • The derived class inherits the attributes and methods of the base class.

  • The derived class can extend the functionality of the base class or override its methods.

  • It promotes code reuse and allows for a hierarchical class structure.


2️⃣ Syntax of Single Inheritance

  • class DerivedClass : accessSpecifier BaseClass {

  •     // Additional members or methods of DerivedClass

  • };


  • DerivedClass inherits from BaseClass.

  • accessSpecifier defines the access level (public, protected, or private) for the members of the base class.


3️⃣ Example: Single Inheritance in C++

  • #include <iostream>

  • using namespace std;


  • // Base Class

  • class Animal {

  • public:

  •     string name;


  •     // Method of the base class

  •     void speak() {

  •         cout << "The animal speaks." << endl;

  •     }

  • };


  • // Derived Class

  • class Dog : public Animal {  // Inheriting from Animal

  • public:

  •     void speak() {  // Method overriding

  •         cout << "The dog barks." << endl;

  •     }


  •     void showName() {

  •         cout << "The dog's name is: " << name << endl;

  •     }

  • };


  • int main() {

  •     Dog dog1;  // Create an object of the derived class

  •     dog1.name = "Buddy";  // Set the name from the base class

  •     dog1.speak();  // Call overridden method in derived class

  •     dog1.showName();  // Call method in derived class


  •     return 0;

  • }


Output:

  • The dog barks.

  • The dog's name is: Buddy


Explanation:

  • Dog is the derived class that inherits from the base class Animal.

  • speak() is overridden in Dog, so the dog-specific version is called when we invoke speak().

  • The name attribute is inherited from Animal and is used in Dog.


4️⃣ Access Specifiers in Inheritance

In C++, the access level of the members of the base class depends on the access specifier used while inheriting:

Public Inheritance:

  • Public members of the base class remain public in the derived class.

  • Protected members of the base class remain protected in the derived class.

  • Private members of the base class are not accessible in the derived class directly.

Private Inheritance:

  • Public and protected members of the base class become private in the derived class.

Protected Inheritance:

  • Public and protected members of the base class become protected in the derived class.


5️⃣ Example of Different Access Specifiers in Inheritance

  • #include <iostream>

  • using namespace std;


  • // Base Class

  • class Base {

  • public:

  •     int publicVar;

  • protected:

  •     int protectedVar;

  • private:

  •     int privateVar;


  • public:

  •     Base() : publicVar(1), protectedVar(2), privateVar(3) {}


  •     void display() {

  •         cout << "Public: " << publicVar << endl;

  •         cout << "Protected: " << protectedVar << endl;

  •         cout << "Private: " << privateVar << endl;

  •     }

  • };


  • // Derived Class (public inheritance)

  • class Derived : public Base {

  • public:

  •     void displayDerived() {

  •         cout << "Accessing public variable: " << publicVar << endl;  // OK

  •         cout << "Accessing protected variable: " << protectedVar << endl;  // OK

  •         // cout << "Accessing private variable: " << privateVar << endl;  // Error

  •     }

  • };


  • int main() {

  •     Derived obj;

  •     obj.displayDerived();  // Accessing the inherited variables in derived class

  •     obj.display();  // Accessing from base class method


  •     return 0;

  • }


Output:

  • Accessing public variable: 1

  • Accessing protected variable: 2

  • Public: 1

  • Protected: 2

  • Private: 3


Explanation:

  • In public inheritance, the publicVar and protectedVar are accessible in the derived class.

  • The privateVar is not accessible because it is private to the base class.


6️⃣ Benefits of Single Inheritance

  1. Code Reusability: The derived class inherits functionality from the base class, reducing code duplication.

  2. Organization: It creates a hierarchical structure and organizes code better.

  3. Extensibility: You can extend base class functionalities in the derived class without modifying the base class.


7️⃣ Summary of Single Inheritance in C++

Feature

Base Class

Derived Class

Access to Members

Public, Protected, Private

Inherited Public/Protected, Not Private

Method Overriding

No

Yes

Constructor

Yes

Yes (default)

Destructor

Yes

Yes

Inheritance Type

N/A

Single


8️⃣ When to Use Single Inheritance?

✅ When a derived class should inherit properties and methods from a single base class.
✅ When you want to extend functionality without modifying the base class.
✅ For creating specialized versions of a general class.

Would you like to learn more about Multiple Inheritance, or explore another topic? Let me know! 😊


Post a Comment

0 Comments