Inheritance in CPP

Inheritance in CPP

Inheritance is a fundamental concept in object-oriented programming (OOP). It allows us to define a new class based on an existing class, inheriting the properties of the existing class and adding new properties or behaviors. In C++, inheritance is implemented using classes and keywords like ‘public’, ‘private’ and ‘protected’.

In this blog, we will explore inheritance in more detail and understand how it can be used in C++.

Introduction to Inheritance

Inheritance is a way to create a new class from an existing class, called the base class. The new class is called the derived class. The derived class inherits the properties of the base class, including its data members and member functions.

In C++, there are three types of inheritance: public, private, and protected. The type of inheritance determines the accessibility of the base class members in the derived class.

Public Inheritance

In public inheritance, the public members of the base class become public members of the derived class, the protected members of the base class become protected members of the derived class, and the private members of the base class are not accessible in the derived class. Here’s an example:

arduinoCopy codeclass Shape {
    public:
        void setWidth(int w) {
            width = w;
        }
        void setHeight(int h) {
            height = h;
        }
    protected:
        int width;
        int height;
};

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

int main() {
    Rectangle r;
    r.setWidth(5);
    r.setHeight(10);
    cout << "Area of the rectangle: " << r.getArea() << endl;
    return 0;
}

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’ functions from the ‘Shape’ class and defines a new function ‘getArea’. The ‘getArea’ function uses the ‘width’ and ‘height’ data members of the ‘Shape’ class to calculate the area of the rectangle.

Private Inheritance

In private inheritance, the public and protected members of the base class become private members of the derived class, and the private members of the base class are not accessible in the derived class. Here’s an example:

csharpCopy codeclass A {
    public:
        void foo() {
            cout << "foo" << endl;
        }
    protected:
        void bar() {
            cout << "bar" << endl;
        }
};

class B: private A {
    public:
        void baz() {
            foo();
            bar();
        }
};

int main() {
    B b;
    b.baz();
    return 0;
}

In this example, the ‘A’ class is the base class, and the ‘B’ class is the derived class. The ‘B’ class inherits the ‘foo’ function from the ‘A’ class and defines a new function ‘baz’. The ‘baz’ function uses the ‘foo’ and ‘bar’ functions of the ‘A’ class to print “foo” and “bar” to the console.

Protected Inheritance

In protected inheritance, the public and protected members of the base class become protected members of the derived class, and the private members of the base class are not accessible in the derived class. Here’s an example:

csharpCopy codeclass A {
    public:
        void foo() {
            cout << "foo" << endl;
        }
    protected:
        void bar() {
            cout << "bar" << endl;
        }
};

class B: protected A {
    public:
        void baz() {
            foo();
            bar

Need of Inheritance

Inheritance is an important concept in object-oriented programming, including C++. It is used to establish a relationship between two classes, where one class is called the base class or parent class, and the other class is called the derived class or child class. The derived class inherits properties and behaviors from the base class, which it can use or modify according to its own requirements.

The main need of inheritance in C++ is code reuse. Inheritance enables you to create new classes that are built upon existing classes. This means that you can reuse the code and functionality of an existing class, without having to write it again from scratch. This can save time and effort in programming, as well as make your code more efficient and easier to maintain.

Another important need of inheritance is to achieve polymorphism. Polymorphism is the ability of an object to take on multiple forms or types. In C++, this is achieved through virtual functions, which are functions declared in the base class and overridden in the derived class. In this way, you can create objects of different classes that can be treated as objects of a common base class, which can be useful for writing more generic and flexible code.

Inheritance also provides a way to model real-world relationships between objects. For example, a car is a type of vehicle, so you could create a Vehicle class and a Car class that inherits from it. This can help to make your code more intuitive and easier to understand, as it mirrors the relationships between objects in the real world.

In summary, inheritance is an important feature of C++ that allows you to reuse code, achieve polymorphism, and model real-world relationships between objects. It is a powerful tool that can help you write more efficient, flexible, and maintainable code.