C++

Inheritance in C++

847
0
Inheritance in C++

Inheritance is one of the fundamental concepts in object-oriented programming (OOP). It allows a class to inherit the properties and behaviors of another class, called the base or parent class. In other words, a derived or child class can be created by inheriting the features of an existing base class.

The derived class can then add its own properties and behaviors, or override the existing ones, without affecting the base class. This way, inheritance promotes code reuse and makes it easier to create and maintain complex software systems.

C++ supports several types of inheritance, including:

  1. Single inheritance
  2. Multiple inheritance
  3. Hierarchical inheritance
  4. Multilevel inheritance
  5. Hybrid inheritance

We’ll discuss each of these types in detail below.

Single Inheritance

Single inheritance is the most common type of inheritance in C++. It involves creating a derived class that inherits the properties and behaviors of a single base class. The derived class can then add its own properties and behaviors as needed.

Let’s take a look at an example of single inheritance:

// 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);

    cout << "Area of the rectangle is: " << rect.getArea() << endl;

    return 0;
}
C++

In this example, the Shape class is the base class, and the Rectangle class is the derived class. The Rectangle class inherits the setWidth() and setHeight() methods from the Shape class, and adds its own getArea() method to calculate the area of a rectangle.

To create a derived class, we use the public keyword followed by the name of the base class. This indicates that the derived class inherits the public members of the base class. In this example, we’re inheriting the setWidth() and setHeight() methods, which are public.

Multiple Inheritance

Multiple inheritance allows a derived class to inherit the properties and behaviors of two or more base classes. This can be useful when you need to combine the features of multiple classes into a single derived class.

Let’s take a look at an example of multiple inheritance:

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

// Base class 2
class Color {
public:
    void setColor(string c) {
        color = c;
    }
protected:
    string color;
};

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

int main() {
    Rectangle rect;

    rect.setWidth(5);
    rect.setHeight(7);
    rect.setColor("red");

    cout << "Area of the rectangle is: " << rect.getArea() << endl;
    cout << "Color of the rectangle is: " << rect.color << endl;

    return 0;
}
C++

In this example, we have two base classes: Shape and Color. The Rectangle class is derived from both of these base classes using the public keyword followed by the names of the base classes. This indicates that the Rectangle class inherits the public members of both base classes.

The Rectangle class adds its own getArea() method to calculate the area of a rectangle, and it also has access to the setWidth() and setHeight() methods inherited from the Shape class, as well as the setColor() method inherited from the Color class. The color variable is protected in the Color class, so it can be accessed by the Rectangle class.

Hierarchical Inheritance

Hierarchical inheritance involves creating multiple derived classes from a single base class. This allows you to create a hierarchy of related classes with different levels of specialization.

Let’s take a look at an example of hierarchical inheritance:

// Base class
class Animal {
public:
    void eat() {
        cout << "I'm eating generic food." << endl;
    }
};

// Derived class 1
class Cat: public Animal {
public:
    void meow() {
        cout << "Meow!" << endl;
    }
};

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

int main() {
    Cat cat;
    Dog dog;

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

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

    return 0;
}
C++

In this example, we have a single base class Animal and two derived classes Cat and Dog. Both Cat and Dog inherit the eat() method from the Animal class, which prints a generic message about eating.

The Cat class adds its own meow() method to print the sound of a cat, while the Dog class adds its own bark() method to print the sound of a dog. This allows you to create specialized classes for different types of animals, while still sharing common functionality in the base class.

Multilevel Inheritance

Multilevel inheritance involves creating a chain of derived classes, where each derived class is itself a base class for another derived class. This allows you to create increasingly specialized classes with each level of inheritance.

Let’s take a look at an example of multilevel inheritance:

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

// Intermediate class
class Car: public Vehicle {
public:
    void drive() {
        cout << "Driving car." << endl;
    }
};

// Derived class
class SportsCar: public Car {
public:
    void accelerate() {
        cout << "Accelerating sports car." << endl;
    }
};

int main() {
    SportsCar car;

    car.start();
    car.drive();
    car.accelerate();

    return 0;
}
C++

In this example, we have a base class Vehicle and an intermediate class Car, which inherits from Vehicle. The Car class adds its own drive() method to represent driving a car.

The SportsCar class is derived from Car, which means it inherits the start() and drive() methods from Vehicle and Car, respectively. The SportsCar class adds its own accelerate() method to represent the unique behavior of a sports car.

This allows you to create increasingly specialized classes, with each level of inheritance adding its own properties and behaviors to the previous level.

Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance, such as multiple inheritance and hierarchical inheritance. In hybrid inheritance, you can create a derived class from multiple base classes and then create additional derived classes from that derived class.

For example, consider the following code:

class A {
public:
    void foo() { cout << "A::foo" << endl; }
};

class B : public A {
public:
    void bar() { cout << "B::bar" << endl; }
};

class C {
public:
    void baz() { cout << "C::baz" << endl; }
};

class D : public B, public C {
public:
    void qux() { cout << "D::qux" << endl; }
};

class E : public D {
public:
    void quux() { cout << "E::quux" << endl; }
};
C++

In this example, we have a class hierarchy that includes four classes: A, B, C, and D. A is the base class of B, and B is one of the base classes of D. C is a completely separate class that is not related to A or B. D is derived from both B and C, and E is derived from D.

D is an example of hybrid inheritance because it inherits from both B and C. E is also an example of hybrid inheritance because it is derived from D, which is itself derived from multiple base classes.

With hybrid inheritance, you can create complex class hierarchies that model relationships between objects in a program. However, as with any type of inheritance, it is important to use hybrid inheritance judiciously and with care to avoid making your programs too complex and hard to maintain.

Conclusion

Inheritance is a powerful feature of object-oriented programming that allows you to create new classes by inheriting properties and behaviors from existing classes. C++ supports multiple types of inheritance, including single, multiple, hierarchical, multilevel, and, hybrid inheritance.

In each type of inheritance, the derived class inherits the properties and behaviors of the base class(es) and can also add its own properties and behaviors.

In C++, inheritance is implemented using the class keyword, followed by a colon and the keyword public, private, or protected, depending on the desired level of access to the inherited members.

Inheritance is a powerful tool for creating efficient, reusable code and for modeling relationships between objects in a program. However, it should be used judiciously and with care, as it can make programs more complex and harder to maintain if overused.

By understanding the different types of inheritance and how they can be used effectively, you can create well-designed and maintainable object-oriented programs in C++.

xalgord
WRITTEN BY

xalgord

Constantly learning & adapting to new technologies. Passionate about solving complex problems with code. #programming #softwareengineering

Leave a Reply