Inheritance in Detail

COS2614 - Programming: Contemporary Concepts · C++ Language Reference

Inheritance in Detail

Inheritance is a key concept in object-oriented programming (OOP). It allows you to create new classes based on existing ones, promoting code reusability and hierarchical relationships. In C++, inheritance helps you to extend the functionality of a base class by creating derived classes. Understanding inheritance is crucial for designing robust and maintainable software.

Key idea: After studying this topic, you should be able to:

  • Define inheritance and its types in C++.
  • Explain the concept of base and derived classes.
  • Implement inheritance in C++ with examples.
  • Identify the benefits of using inheritance in your programs.

What is Inheritance?

Inheritance is a mechanism that allows one class (the derived class) to inherit properties and behaviours (methods) from another class (the base class). This relationship creates a hierarchy of classes.

Types of Inheritance

In C++, there are several types of inheritance:

  • Single Inheritance: A derived class inherits from one base class.
  • Multiple Inheritance: A derived class inherits from more than one base class.
  • Multilevel Inheritance: A derived class is created from another derived class.
  • Hierarchical Inheritance: Multiple derived classes inherit from a single base class.

Base and Derived Classes

The base class is the class whose properties and methods are inherited. The derived class is the class that inherits from the base class. For example:

class Animal {  // Base class
public:
    void eat() {
        cout << "Eating" << endl;
    }
};

class Dog : public Animal {  // Derived class
public:
    void bark() {
        cout << "Barking" << endl;
    }
};

In this example, the Dog class inherits from the Animal class. The Dog class can use the eat method from the Animal class.

Access Specifiers

In C++, you can control the access level of inherited members using access specifiers:

  • public: Members are accessible from outside the class.
  • protected: Members are accessible in the derived class and its subclasses but not from outside.
  • private: Members are not accessible in the derived class.

For example:

class Base {
protected:
    int x;
};

class Derived : public Base {
public:
    void setX(int val) {
        x = val;  // Accessible because x is protected
    }
};

Benefits of Inheritance

Inheritance provides several advantages:

  • Code Reusability: You can reuse existing code, reducing redundancy.
  • Improved Maintainability: Changes in the base class automatically propagate to derived classes.
  • Polymorphism: You can use base class pointers to refer to derived class objects, allowing for dynamic method binding.

Example of Inheritance

Here is a complete example demonstrating inheritance in C++:

#include 
using namespace std;

class Vehicle {  // Base class
public:
    void start() {
        cout << "Vehicle starting" << endl;
    }
};

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

int main() {
    Car myCar;
    myCar.start();  // Inherited method
    myCar.drive();  // Own method
    return 0;
}

In this example, the Car class inherits from the Vehicle class. The Car object can call both the start method from the Vehicle class and its own drive method.

Watch out: Be careful with multiple inheritance. It can lead to ambiguity if two base classes have methods with the same name. Always specify which base class method to use in such cases.

Summary

  • Inheritance allows a class to inherit properties and methods from another class.
  • Base and derived classes form a hierarchy.
  • Access specifiers control the visibility of inherited members.
  • Inheritance promotes code reusability and maintainability.

Check your understanding

  1. What is the difference between a base class and a derived class?
  2. Explain the concept of multiple inheritance and its potential issues.
  3. How do access specifiers affect the visibility of inherited members?
  4. Provide an example of multilevel inheritance in C++.