Understanding Inheritance in Object-Oriented System Design Using C++(lecture-7)

Introduction

In object-oriented programming (OOP), inheritance is a fundamental concept that enables code reuse, modularity, and a hierarchical relationship among classes. It allows a class to derive properties and behaviors (methods) from another class, promoting reusability and scalability in large software systems.

This blog will explore inheritance in C++, its types, and how it is used in Object-Oriented System Design (OOSD).


What is Inheritance?

Inheritance is a mechanism in OOP that allows one class, known as the derived or child class, to inherit attributes and methods from another class, called the base or parent class. The derived class can extend or modify the functionality of the base class without altering the original code.

Inheritance enables:

  1. Code Reusability: By inheriting, you can reuse the properties and behaviors of an existing class, reducing code duplication.
  2. Extensibility: You can extend the functionality of a base class by adding new features in the derived class.
  3. Maintainability: Changes in the base class propagate to derived classes, making it easier to maintain and update code.

Syntax of Inheritance in C++

cppCopy codeclass BaseClass {
  // Base class members
};

class DerivedClass : public BaseClass {
  // Derived class members
};

Here, DerivedClass inherits from BaseClass. The keyword public indicates that the public members of BaseClass will remain public in DerivedClass. Other access specifiers like private and protected can also be used, which will be discussed later.


Types of Inheritance in C++

C++ supports multiple types of inheritance, each serving different design purposes:

  1. Single Inheritance: A derived class inherits from only one base class.
  2. Multiple Inheritance: A derived class inherits from more than one base class.
  3. Multilevel Inheritance: A class inherits from a derived class, forming a chain of inheritance.
  4. Hierarchical Inheritance: Multiple classes inherit from a single base class.
  5. Hybrid Inheritance: A combination of two or more types of inheritance.

1. Single Inheritance

In single inheritance, a class derives from a single base class.

cppCopy code#include <iostream>
using namespace std;

class Animal {
  public:
    void eat() {
      cout << "This animal is eating." << endl;
    }
};

class Dog : public Animal {
  public:
    void bark() {
      cout << "The dog is barking." << endl;
    }
};

int main() {
  Dog myDog;
  myDog.eat();  // Inherited from Animal
  myDog.bark(); // Specific to Dog
  return 0;
}

Here, Dog inherits the eat() method from the Animal class and adds its own bark() method.

2. Multiple Inheritance

In multiple inheritance, a class derives from more than one base class.

cppCopy code#include <iostream>
using namespace std;

class Mammal {
  public:
    void feed() {
      cout << "This mammal is feeding." << endl;
    }
};

class Bird {
  public:
    void fly() {
      cout << "This bird is flying." << endl;
    }
};

class Bat : public Mammal, public Bird {
  public:
    void useEcholocation() {
      cout << "The bat is using echolocation." << endl;
    }
};

int main() {
  Bat myBat;
  myBat.feed();           // Inherited from Mammal
  myBat.fly();            // Inherited from Bird
  myBat.useEcholocation(); // Specific to Bat
  return 0;
}

Here, the Bat class inherits features from both Mammal and Bird, making it an example of multiple inheritance.

3. Multilevel Inheritance

In multilevel inheritance, a class is derived from another derived class.

cppCopy code#include <iostream>
using namespace std;

class Vehicle {
  public:
    void start() {
      cout << "Vehicle started." << endl;
    }
};

class Car : public Vehicle {
  public:
    void drive() {
      cout << "Car is driving." << endl;
    }
};

class SportsCar : public Car {
  public:
    void boost() {
      cout << "Sports car boosting." << endl;
    }
};

int main() {
  SportsCar myCar;
  myCar.start();  // Inherited from Vehicle
  myCar.drive();  // Inherited from Car
  myCar.boost();  // Specific to SportsCar
  return 0;
}

In this example, SportsCar inherits from Car, which in turn inherits from Vehicle. This is a chain of inheritance.


Access Specifiers in Inheritance

The access specifiers public, protected, and private control the accessibility of base class members in the derived class:

  1. Public Inheritance: Public members of the base class remain public in the derived class.
  2. Protected Inheritance: Public and protected members of the base class become protected in the derived class.
  3. Private Inheritance: All members of the base class become private in the derived class.

Understanding these specifiers helps in controlling access to class members and designing secure and modular code.


Key Concepts in Inheritance

  1. Overriding: Derived classes can override base class methods to provide specialized behavior.
  2. Polymorphism: Inheritance supports polymorphism, allowing derived classes to be treated as base classes, enabling dynamic method binding.
  3. Virtual Functions: In C++, virtual functions allow for polymorphic behavior, where the derived class’s method can be called even through a base class pointer.

Conclusion

Inheritance is a powerful feature of Object-Oriented System Design that allows developers to create hierarchical relationships between classes, leading to more reusable and maintainable code. By understanding the different types of inheritance and their proper usage, you can leverage this concept in C++ to design robust software systems.

Mastering inheritance is essential for creating flexible, scalable, and modular applications, making it a cornerstone of effective C++ programming

Scroll to Top