1
Posted March 3, 2011 by Spyros in C/C++ Programming
 
 

How to use Inheritance in C++ Programming

inheritance
inheritance

C++ is rich because of its object oriented programming concepts. Inheritance is the ability to create a hierarchy of classes where a specific class inherits the properties of a generalized class. For example, if we consider Polygon to be a generic class, then shapes like Triangle and Rectangle can be regarded as specific classes. Surely, all the polygons have attributes like height and width, whereas the way to calculate area is different for different shapes.

Base Class and Derived Class

The generic class is termed as base class and the class which inherits from a base class is known as derived class. Normally, a derived class can access all the methods and attributes of the base class. If required, using some keywords, the derived class can be restricted from accessing the attributes of the base class. The keywords used to control the access are public, protected and private. If an attribute or method is declared as public, it can be accessed by the derived class as well as outside functions. That is, if it is part of an instance of the same class. If it is declared as protected, it can be accessed by any derived classes only. If the private keyword is used, the access is completely restricted by all other classes including any derived classes. A : (full colon) is used to indicate that a derived class is inheriting from a base class. The constructor and destructor of the base class cannot be inherited by a derived class.

Multiple Inheritance

It is also possible for a class to inherit from more than one class simultaneously. This is known as multiple inheritance. For example, suppose there are three classes : electronic products, educational products and computer. Here, a computer can inherit both from electronic products class and educational products class.

Now we will consider an example to know in detail of all the concepts explained above.

class Polygon
{
    protected int width, height;
    public void setParameters (int w,int h)
    {
        width = w;
        height = h;
    }
};

class Rectangle : public Polygon
{
    public int calculateArea ()
    {
        return (width*height);
    }
};

class Triangle : public Polygon
{
    public int calculateArea ()
    {
        return( (width*height)/2);
    }
};

int main ()
{
    Rectangle rect;
    Triangle trgl;
    rect. setParameters (4,5);
    trgl. setParameters (4,5);
    cout << rect. calculateArea () << endl;
    cout << trgl. calculateArea () << endl;
    return 0;
}

The statements “Class Rectangle : public Polygon” and “class Triangle : public Polygon” indicate that classes Rectangle and Triangle inherit from the class Polygon. Polygon is the base class and Rectangle, Triangle are derived classes. In the above example, the attributes width and height are declared as protected inside the base class, as their access is intended to be within the derived class alone. Those attributes cannot be accessed within the main function directly. The method calculateArea() is defined differently in both the classes, Rectangle and Triangle.

Advantages of inheritance

The main advantage of inheritance is re-usability. We don’t have to again and again create classes with similar attributes and functions. Once a base class is created and compiled, it is ready to reuse. Even if there are some new features to be added we can inherit it from the existing class and add the required methods or attributes. This consequently saves time and effort for the programmer.


Spyros