March 15, 2023

Function Overriding

Posted on March 15, 2023  •  4 minutes  • 784 words

Our knowledge of inheritance begins with the fact that we can create derived classes from a base class, which is one of the core features of OOP. A derived class inherits the features of a base class.

Assume that the same function is defined in both the derived class and the based class. When we call this function using the object of the derived class, the derived class’s function is executed.

In C++, this is known as function overriding. A derived class’s function overrides its base class’s function (simply re-defining the base class function).

#include<iostream>

class Base
{
public:
    void myFunction1()
    {
        std::cout << "Base::myFunction1" << std::endl;
    }
};

class Child : public Base
{
public:
    void myFunction1()
    {
        std::cout << "Child::myFunction1" << std::endl;
    }
};

Here myFunction1() in the child class overrides (re-defining) the base class function myFunction1()

int main(int argc, char const *argv[])
{
    Base b;
    b.myFunction1();

    Child c;
    c.myFunction1();

    return 0;
}

Accessing Base class function by a child object

It is also possible to access the overridden function in the base class by using :: (scope resolution operator)

int main(int argc, char const *argv[])
{
    Child c;
    c.myFunction1(); // accessing child class function
    c.Base::myFunction1(); // accessing base class function

    return 0;
}

Example

We can re-design the code in the Inheritance by using overriding

  1. In the base class we can define two functions getArea() and getPerimeter() and remove the unwanted class variables area and perimeter.
class Shape
{
public:
    double getArea()
    {
        return 0.0;
    }

    double getPerimeter()
    {
        return 0.0;
    }
};
  1. We can create a child class Circle inheriting from Shape. And Circle has its own property radius. Also we can redefine the getArea() and getPerimeter() functions by Circle’s formula.
class Circle : public Shape
{
public:
    Circle(double rad)
    {
        radius = rad;
    }

    double getArea()
    {
        return (3.14 * radius * radius);
    }

    double getPerimeter()
    {
        return (2 * 3.14 * radius);
    } 
private:
    double radius;
};
  1. Similarly we can create two more classes Rectangle and Triangle
class Rectangle : public Shape
{
public:
    Rectangle(double len, double wid)
    {
        length = len;
        width = wid;
    }

    double getArea()
    {
        return (length * width);
    }

    double getPerimeter()
    {
        return (2 * (length + width));
    }
private:
    double length, width;
};
class Triangle : public Shape
{
public:
    Triangle(double s1, double s2, double s3)
    {
        side1 = s1;
        side2 = s2;
        side3 = s3;
    }

    double getArea()
    {
        double s = (side1 + side2 + side3) / 2;
        double area = sqrt(s * (s-side1) * (s-side2) * (s-side3));
        return area;
    }

    double getPerimeter()
    {
        return (side1 + side2 + side3);
    }
private:
    double side1, side2, side3;
};
  1. Finally we can create a main function to test the implementations.
int main(int argc, char const *argv[])
{
    Circle c1(10);
    std::cout << "Circle Area : " << c1.getArea() << std::endl;
    std::cout << "Circle Perimeter : " << c1.getPerimeter() << std::endl << std::endl;

    Rectangle r1(10,20);
    std::cout << "Rectangle Area : " << r1.getArea() << std::endl;
    std::cout << "Rectangle Perimeter : " << r1.getPerimeter() << std::endl << std::endl;

    Triangle t1(3,4,5);
    std::cout << "Triangle Area : " << t1.getArea() << std::endl;
    std::cout << "Triangle Perimeter : " << t1.getPerimeter() << std::endl << std::endl;
    
    return 0;
}

Full Program

#include <iostream>
#include <cmath>

class Shape
{
public:
    double getArea()
    {
        return 0.0;
    }

    double getPerimeter()
    {
        return 0.0;
    }
};

class Circle : public Shape
{
public:
    Circle(double rad)
    {
        radius = rad;
    }

    double getArea()
    {
        return (3.14 * radius * radius);
    }

    double getPerimeter()
    {
        return (2 * 3.14 * radius);
    }
private:
    double radius;
};

class Rectangle : public Shape
{
public:
    Rectangle(double len, double wid)
    {
        length = len;
        width = wid;
    }

    double getArea()
    {
        return (length * width);
    }

    double getPerimeter()
    {
        return (2 * (length + width));
    }
private:
    double length, width;
};

class Triangle : public Shape
{
public:
    Triangle(double s1, double s2, double s3)
    {
        side1 = s1;
        side2 = s2;
        side3 = s3;
    }

    double getArea()
    {
        double s = (side1 + side2 + side3) / 2;
        double area = sqrt(s * (s-side1) * (s-side2) * (s-side3));
        return area;
    }

    double getPerimeter()
    {
        return (side1 + side2 + side3);
    }
private:
    double side1, side2, side3;
};

int main(int argc, char const *argv[])
{
    Circle c1(10);
    std::cout << "Circle Area : " << c1.getArea() << std::endl;
    std::cout << "Circle Perimeter : " << c1.getPerimeter() << std::endl << std::endl;

    Rectangle r1(10,20);
    std::cout << "Rectangle Area : " << r1.getArea() << std::endl;
    std::cout << "Rectangle Perimeter : " << r1.getPerimeter() << std::endl << std::endl;

    Triangle t1(3,4,5);
    std::cout << "Triangle Area : " << t1.getArea() << std::endl;
    std::cout << "Triangle Perimeter : " << t1.getPerimeter() << std::endl << std::endl;
    
    return 0;
}
Follow me

I work on everything coding and share developer memes