Inheriting from Base Classes

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to acquire the properties and behaviors of another class. In C++, you can achieve inheritance by creating a derived class from a base class. The derived class, also known as a subclass, inherits the members (i.e., attributes and methods) of the base class, also known as the super class or parent class. This article will explore the concept of inheriting from base classes in C++.

Syntax of Inheritance

To create a derived class, you use the following syntax in C++:

class DerivedClass : access-specifier BaseClass
{
    // members of the derived class
};

Here, DerivedClass is the name of the derived class, access-specifier determines the accessibility of the inherited members, and BaseClass is the name of the base class being inherited from.

The access-specifier can be one of three types:

  1. Private (private): With private inheritance, all members of the base class become private members of the derived class. It means that the derived class can access the base class members only through its own member functions.
  2. Protected (protected): With protected inheritance, all members of the base class become protected members of the derived class. It means that the derived class and its derived classes can access the base class members directly.
  3. Public (public): With public inheritance, all members of the base class become public members of the derived class. It means that the derived class and any other code can access the base class members directly.

Accessing Inherited Members

Once you have derived a class from a base class, you can access the inherited members using the member access operator (.) or the pointer-to-member access operator (->).

For example, consider a base class Shape with a member function display:

class Shape
{
public:
    void display()
    {
        cout << "Displaying a shape" << endl;
    }
};

You can create a derived class Rectangle that inherits from Shape and adds its own member function:

class Rectangle : public Shape
{
public:
    void calculateArea()
    {
        cout << "Calculating area of rectangle" << endl;
    }
};

To access the inherited display function in the Rectangle class, you can use the following code:

Rectangle rectangle;
rectangle.display();

Overriding Base Class Members

One of the key features of inheritance is the ability to override base class members in the derived class. This allows the derived class to provide its own implementation of a member function or to modify the behavior of an inherited member function.

To override a base class member function in the derived class, you need to redefine the function using the same signature. To indicate the intention to override a member function, you can use the override keyword (available in C++11) as a best practice.

For example, let's enhance our Rectangle class by overriding the display member function:

class Rectangle : public Shape
{
public:
    void display() override
    {
        cout << "Displaying a rectangle" << endl;
    }

    void calculateArea()
    {
        cout << "Calculating area of rectangle" << endl;
    }
};

Now, when you call the display function on an instance of Rectangle, it will execute the overridden implementation instead of the base class implementation:

Rectangle rectangle;
rectangle.display(); // Output: Displaying a rectangle

Conclusion

Inheritance is a powerful mechanism provided by C++ to establish relationships between classes, enabling code reuse and promoting the creation of well-structured programs. By inheriting from base classes, derived classes can acquire the properties and behaviors of their parents, facilitating code organization, modularity, and extensibility. Understanding and properly utilizing inheritance is crucial for developers working with the C++ programming language.


noob to master © copyleft