C++ Inheritance

Introduction

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class to inherit properties and behaviors (methods) from another class. The class that inherits is called the derived class (or subclass), and the class from which it inherits is called the base class (or superclass). Inheritance promotes code reusability and establishes a natural hierarchy between classes.

Types of Inheritance

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

Syntax for Inheritance

Basic Syntax

class BaseClass {
    // Base class members
};

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

Example: Single Inheritance

#include <iostream>
using namespace std;

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

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

int main() {
    Dog dog;
    dog.eat();  // Inherited from base class
    dog.bark(); // Defined in derived class

    return 0;
}

Output

Eating...
Barking...

Explanation

  • The Animal class is the base class with a method eat.
  • The Dog class is the derived class with a method bark and inherits from the Animal class using the public access specifier.

Access Specifiers and Inheritance

The access specifier used in inheritance determines the accessibility of the base class members in the derived class.

  • Public Inheritance: Public and protected members of the base class remain public and protected in the derived class.
  • Protected Inheritance: Public and protected members of the base class become protected in the derived class.
  • Private Inheritance: Public and protected members of the base class become private in the derived class.

Example: Access Specifiers

#include <iostream>
using namespace std;

// Base class
class Base {
public:
    int publicVar;
protected:
    int protectedVar;
private:
    int privateVar;
};

// Publicly derived class
class PublicDerived : public Base {
public:
    void display() {
        publicVar = 1;        // Accessible
        protectedVar = 2;     // Accessible
        // privateVar = 3;    // Not accessible
        cout << "Public Derived: publicVar = " << publicVar << ", protectedVar = " << protectedVar << endl;
    }
};

// Privately derived class
class PrivateDerived : private Base {
public:
    void display() {
        publicVar = 4;        // Accessible but becomes private
        protectedVar = 5;     // Accessible but becomes private
        // privateVar = 6;    // Not accessible
        cout << "Private Derived: publicVar = " << publicVar << ", protectedVar = " << protectedVar << endl;
    }
};

int main() {
    PublicDerived publicObj;
    publicObj.display();

    PrivateDerived privateObj;
    privateObj.display();

    return 0;
}

Output

Public Derived: publicVar = 1, protectedVar = 2
Private Derived: publicVar = 4, protectedVar = 5

Explanation

  • In PublicDerived, public and protected members of Base remain public and protected, respectively.
  • In PrivateDerived, public and protected members of Base become private in the derived class.

Multilevel Inheritance

Multilevel inheritance is when a class is derived from another derived class.

Example: Multilevel Inheritance

#include <iostream>
using namespace std;

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

// Derived class
class Mammal : public Animal {
public:
    void walk() {
        cout << "Walking..." << endl;
    }
};

// Further derived class
class Dog : public Mammal {
public:
    void bark() {
        cout << "Barking..." << endl;
    }
};

int main() {
    Dog dog;
    dog.eat();   // Inherited from Animal
    dog.walk();  // Inherited from Mammal
    dog.bark();  // Defined in Dog

    return 0;
}

Output

Eating...
Walking...
Barking...

Explanation

  • The Animal class is the base class.
  • The Mammal class is derived from Animal.
  • The Dog class is derived from Mammal.

Hierarchical Inheritance

Hierarchical inheritance is when multiple derived classes inherit from a single base class.

Example: Hierarchical Inheritance

#include <iostream>
using namespace std;

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

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

// Another derived class
class Cat : public Animal {
public:
    void meow() {
        cout << "Meowing..." << endl;
    }
};

int main() {
    Dog dog;
    Cat cat;

    dog.eat();
    dog.bark();

    cat.eat();
    cat.meow();

    return 0;
}

Output

Eating...
Barking...
Eating...
Meowing...

Explanation

  • The Animal class is the base class.
  • The Dog and Cat classes are derived from Animal.

Multiple Inheritance

Multiple inheritance is when a class is derived from more than one base class.

Example: Multiple Inheritance

#include <iostream>
using namespace std;

// Base class 1
class Person {
public:
    void speak() {
        cout << "Speaking..." << endl;
    }
};

// Base class 2
class Employee {
public:
    void work() {
        cout << "Working..." << endl;
    }
};

// Derived class
class Manager : public Person, public Employee {
public:
    void manage() {
        cout << "Managing..." << endl;
    }
};

int main() {
    Manager mgr;
    mgr.speak();
    mgr.work();
    mgr.manage();

    return 0;
}

Output

Speaking...
Working...
Managing...

Explanation

  • The Person and Employee classes are base classes.
  • The Manager class is derived from both Person and Employee.

Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance.

Example: Hybrid Inheritance

#include <iostream>
using namespace std;

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

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

// Derived class 2
class Boat : public Vehicle {
public:
    void sail() {
        cout << "Boat sailing..." << endl;
    }
};

// Multiple inheritance
class AmphibiousVehicle : public Car, public Boat {
public:
    void amphibiousDrive() {
        cout << "Amphibious vehicle driving..." << endl;
    }
};

int main() {
    AmphibiousVehicle amph;
    amph.start(); // This will cause ambiguity

    return 0;
}

Output

Error: request for member 'start' is ambiguous

Explanation

  • The Vehicle class is the base class.
  • The Car and Boat classes are derived from Vehicle.
  • The AmphibiousVehicle class is derived from both Car and Boat.
  • Calling start from AmphibiousVehicle causes ambiguity because it inherits from two Vehicle instances.

Resolving Ambiguity in Multiple Inheritance

To resolve ambiguity in multiple inheritance, we can use scope resolution.

#include <iostream>
using namespace std;

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

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

// Derived class 2
class Boat : public Vehicle {
public:
    void sail() {
        cout << "Boat sailing..." << endl;
    }
};

// Multiple inheritance
class AmphibiousVehicle : public Car, public Boat {
public:
    void amphibiousDrive() {
        cout << "Amphibious vehicle driving..." << endl;
    }
};

int main() {
    AmphibiousVehicle amph;
    amph.Car::start(); // Resolve ambiguity by specifying the class

    return 0;
}

Output

Vehicle starting...

Explanation

  • amph.Car::start() specifies which start method to call, resolving the ambiguity.

Example Programs

Example 1: Shape Inheritance

#include <iostream>
using namespace std;

// Base class
class Shape {
public:
    void setWidth(int w) {
        width = w;
    }
    void setHeight(int h) {


 height = h;
    }
protected:
    int width;
    int height;
};

// Derived class
class Rectangle : public Shape {
public:
    int getArea() {
        return (width * height);
    }
};

int main() {
    Rectangle rect;
    rect.setWidth(5);
    rect.setHeight(7);

    // Print the area of the object.
    cout << "Total area: " << rect.getArea() << endl;

    return 0;
}

Output

Total area: 35

Explanation

  • The Shape class is the base class with width and height attributes.
  • The Rectangle class is derived from Shape and adds a method to calculate the area.

Example 2: Animal Inheritance

#include <iostream>
using namespace std;

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

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

// Another derived class
class Cat : public Animal {
public:
    void meow() {
        cout << "Meowing..." << endl;
    }
};

int main() {
    Dog dog;
    Cat cat;

    dog.eat();
    dog.bark();

    cat.eat();
    cat.meow();

    return 0;
}

Output

Eating...
Barking...
Eating...
Meowing...

Explanation

  • The Animal class is the base class with an eat method.
  • The Dog and Cat classes are derived from Animal and add their own methods.

Conclusion

Inheritance in C++ allows a class to inherit properties and behaviors from another class, promoting code reusability and establishing a natural hierarchy. This chapter covered the different types of inheritance, including single, multiple, multilevel, hierarchical, and hybrid inheritance, and explained access specifiers in the context of inheritance. Example programs demonstrated how to implement and use inheritance in C++. Understanding inheritance is crucial for creating extensible and maintainable object-oriented programs.

Leave a Comment

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

Scroll to Top