Instagram
youtube
Facebook
Twitter

Inheritance in OOPS

Inheritance

Inheritance allows you to create a new class( derived class) that inherits properties( data members and member functions) from an existing class (base class).

The derived class can extend or modify the behaviour of the base class and also add new functionality.

Access specifiers( public, protected, and private) determine the visibility of inherited members in the derived class.

Inheritance is a fundamental concept in object-oriented programming that allows you to create a new class based on an existing one, inheriting its attributes (data members) and behaviours (member functions). The new class is called the derived class or subclass, and the existing class is called the base class or superclass.

Base Class and Derived Class:

  • Base Class (Superclass): The class whose attributes and methods are inherited by another class, serves as the foundation for the derived classes.
  • Derived Class (Subclass): The class that inherits the attributes and methods from the base class, can also have additional attributes and methods from the base class. It can also have additional attributes and methods specific to itself.

 

class Base {

    // Base class members

};

class Derived : public Base {

    // Derived class members

};

 

Access Control in Inheritance:

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

 

class Vehicle {

public:

    string brand;


    void honk() {

        cout << "Honk honk!" << endl;

    }

};

class Car : public Vehicle {

public:

    string model;

};

class Bicycle : public Vehicle {

public:

    int numGears;

};

 

Types of Inheritance:

Inheritance enables code reuse, extension, and the implementation of the "is a" relationship between classes. C++ supports multiple types of inheritance.

  • Single Inheritance: A derived class inherits from a single base class.
class Animal {

public:

    void eat() { /* ... */ }

};

class Dog : public Animal {

public:

    void bark() { /* ... */ }

};

 

  • Multiple Inheritance:Multiple inheritance is a feature in object-oriented programming languages, including C++, that allows a class to inherit properties and behaviours from multiple base classes. In C++, a class can inherit from multiple classes, which can introduce complex relationships and challenges. Here’s an explanation of multiple inheritance in C++, along with some examples and considerations.

Syntax for Multiple Inheritance:

class BaseClass1 {

    };

class BaseClass2 {

    };

class DerivedClass : public BaseClass1, public BaseClass2 {

    };
  • Multilevel Inheritance: A derived class inherits from another derived class.
class Vehicle {

public:

    void start() { /* ... */ }

};

class Car : public Vehicle {

public:

    void drive() { /* ... */ }

};

class SportsCar : public Car {

public:

    void accelerate() { /* ... */ }

};
  • Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
class Shape {

public:

    void draw() { /* ... */ }

};

class Circle : public Shape {

public:

    void drawCircle() { /* ... */ }

};

class Square : public Shape {

public:

    void drawSquare() { /* ... */ }

};

Inheritance allows the derived class to inherit the members of the base class. This includes both public and protected members. Private members of the base class aren’t directly accessible in the derived class, but they can be accessed through public or protected member functions of the base class.

Derived classes can also override (rewrite) base class functions with their own implementations. This is known as function overriding and allows you to provide specific behaviour in the derived class.

class Shape {

public:

    virtual void draw() {

        std::cout << "Drawing a shape." << std::endl;

    }

};

class Circle : public Shape {

public:

    void draw() override {

        std::cout << "Drawing a circle." << std::endl;

    }

};

Inheritance plays a significant role in achieving code reusability and creating a clear and organised class hierarchy. Still, it’s important to use inheritance judiciously to avoid excessive coupling and promote maintainability in your codebase.

  • Diamond Inheritance: Diamond inheritance is a specific scenario in object-oriented programming, especially in languages like C++, where a class inherits from two classes that share a common base class. This can lead to ambiguity and confusion in the inheritance hierarchy, often referred to as the "diamond problem". The diamond problem arises when multiple paths in the inheritance hierarchy lead to the same base class.
class A {

public:

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

};

class B : public virtual A {};

class C : public virtual A {};

class D : public B, public C {};

int main() {

    D d;

    d.foo();  

    return 0;

}

 

Overriding:Derived classes can provide their own implementation of the base class method. To override a method, use the ‘virtual’ keyword in the base class and the override keyword in the derived class.

class Shape {

public:

    virtual void draw() {

        cout << "Drawing a shape." << endl;

    }

};

class Circle : public Shape {

public:

    void draw() override {

        cout << "Drawing a circle." << endl;

    }

};

 

Illustration:

class Student: public Person {

private:

std::string studentId;

public:

void setStudentId( const std::string& id ) {

studentId = id;

}

Std::string getStudentId() const {

return studentId;

}

};