Inheritance in C++ - Part I

What is C++ way of becoming rich?

    Ans : Inheritance  :)


In fact inheritance is a mechanism in OOP languages which stops us from reinventing the wheels and use of existing classes and create new classes from them according to our requirement. 

We already have so many codes in our libraries. We use them  in procedure oriented languages. But in OOP, we can add more features to them, by deriving them. If we already have a class called shape, we can derive a class circle which has extra data radius. We can derive a class called rectangle with two data members length and width etc. We can create a class square by inheriting rectangle and add a restriction here that length = width.

Advantages of Inheritance

Simple - you do not reinvent the wheel. You can save so much time and energy. And also  you do not have to modify the original class.

Terminology

The general class is called base class or super class. And new specialized class is called  derived class or  sub class. If we redefine any methods of base class, the new methods are called overridden methods

How to create derived classes


class shape
{
    int color;     
private:
    float calculate_area();
    void set_color(int color);
};
float shape::calculate_area()
{
    float area = 1;//currently a dummy value
    return area;
}

Now shape class is the  base class. We know that circle, rectangle, triangle etc can be derived from our shape class.

class circle: public shape
{
   float radius;
private:
    void set_radius(float radius);
};
void circle::set_radius(float radius)
{
    this->radius = radius;
}



Let us look at first line of circle class class circle:public shape. The syntax for derived class is class derived:public base   . public indicates that the public method of inheritance is used where public methods of base class remain public in derived class.

Circle class will have 3 data members color, area and radius, first two inherited from shape class and third one a new member. This class also will have 3 methods, calculate_area(),set_color() and set_radius. Let us try to use these in main method.

#include<iostream>
using namespace std;
int main()
{
      circle c1;//line 1
      c1.set_color(3);//line 2
      c1.set_radius(7);//line 3
      float area = c1.calculate_area();//line 4
      cout<<"Area of circle is "<<area;//line 5
      return 0;
}
 
C1 a circle object is created in line 1. What will be the sizeof this object? Do you say 4 bytes? Think again. 4 bytes for radius, 4 more for color, because c1 has inherited color from base class.
 
Using lines 1-3 we have created a circle object, assigned color 3 to it and set radius as 7. In line 4 we try to find its area. This calls the base class (shape) method calculate_area and returns, not 154 but 1. See our programs can not read minds. We have give area =1  because we did not know how to calculate area of unknown shape and so it will be 1.

Now we need to change this calculation method. Do we write a new method with a new name? No. We write a method with same name, same signature, but different body.

float circle::calculate_area()
{
     float area = 22.0/7 * radius*radius;
     return area;
}

The above method which is redefined in derived class is called overridden method. So a derived class uses overridden function if present, base class function if overridden function is not present. 

Now if we compile and run the program, we should get output as 
Area of cirlce is 154

Now it is your turn to use inheritance in your program.

1> Write a program with base class rectangle and derived class square. Write methods to calculate area.
2> Write a program with base class vehicle and derived classes car, bike and scooter.

In the next post we will see about protected members and their significance among other things about inheritance

Comments

Popular Posts