c-plus-plus
  1. c-plus-plus-overloading

C++ Polymorphism Overloading

Polymorphism overloading is a powerful concept in C++ that allows us to use the same function name to perform different operations based on the number and type of arguments passed to it. This feature of C++ is called polymorphism.

Syntax

return_type function_name(parameters) {
   // implementation
}

return_type function_name(parameters1) {
   // implementation for parameters1
}

return_type function_name(parameters1, parameters2) {
   // implementation for parameters1 and parameters2
}

// and so on...

Example

#include <iostream>
using namespace std;

class Shape {
   private:
      int width, height;

   public:
      Shape(int a = 0, int b = 0) {
         width = a;
         height = b;
      }
      
      int area() {
         cout << "Parent class area :" << endl;
         return 0;
      }

      int getArea() {
         return area();
      }
};

class Rectangle: public Shape {
   public:
      Rectangle(int a = 0, int b = 0):Shape(a, b) { }
      
      int area () {
         cout << "Rectangle class area :" << endl;
         return (width * height);
      }
};

class Triangle: public Shape{
   public:
      Triangle(int a = 0, int b = 0):Shape(a, b) { }

      int area () {
         cout << "Triangle class area :" << endl;
         return (width * height / 2);
      }
};

int main() {
   Shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);

   // store the address of Rectangle
   shape = &rec;

   // call rectangle area.
   shape->getArea();

   // store the address of Triangle
   shape = &tri;

   // call triangle area.
   shape->getArea();
   
   return 0;
}

Output

Parent class area :
Parent class area :

Explanation

In the above example, we have a base class called Shape and two derived classes called Rectangle and Triangle. The Shape class has a getArea() function, and the Rectangle and Triangle classes override the area() function of the Shape class.

We create two objects of the Rectangle and Triangle classes, and then we call the getArea() function on them using a pointer of the Shape class, which is a technique called polymorphism. The getArea() function in turn calls the area() function of the object it points to, and the appropriate area() function gets executed based on the type of object pointed to by the Shape pointer.

In the first call, the shape pointer points to the rec object of the Rectangle class. So, when getArea() function is called, it calls the area() function of the Rectangle class, which calculates and returns the area of the rectangle. In the second call, the shape pointer points to the tri object of the Triangle class. So, when getArea() function is called, it calls the area() function of the Triangle class, which calculates and returns the area of the triangle.

Use

Polymorphism overloading is useful in situations where we want to perform the same operation with different types and numbers of arguments. It simplifies the code, by avoiding the need to create different functions with different names for each variant of the operation.

Important Points

  • Polymorphism overloading can be achieved in C++ using function overloading and operator overloading.
  • Function overloading involves defining two or more functions with the same name, but with different types or number of arguments. The function to be called is determined by the type and number of arguments passed to it during the call.
  • Operator overloading involves defining a new meaning for an existing operator, based on the type and number of operands used.
  • Polymorphism overloading is a powerful concept in C++, but it can also lead to confusion and difficulty in understanding the code if overused or misused.

Summary

  • Polymorphism overloading in C++ allows us to use the same function name to perform different operations based on the type and number of arguments passed to it.
  • Polymorphism overloading can be achieved in C++ using function overloading and operator overloading.
  • Polymorphism overloading simplifies the code and improves its readability by avoiding the need to create different functions with different names for each variant of the operation.
  • Polymorphism overloading should be used judiciously to avoid confusion and difficulty in understanding the code.
Published on: