c-plus-plus
  1. c-plus-plus-types-of-polymorphism

C++ Programs: Types of Polymorphism

Polymorphism is one of the core features of object-oriented programming languages like C++. It refers to the ability of an object to take on many forms. In C++, polymorphism is achieved through two mechanisms: function overloading and virtual functions. In this page, we will discuss both types of polymorphism in detail.

Function Overloading

Function overloading is a type of polymorphism in which multiple functions with the same name but different parameters are defined. The appropriate function is called based on the number, type, and order of the arguments passed to it.

Syntax

return_type function_name(parameters);
return_type function_name(parameters1, parameters2);
return_type function_name(parameters1, parameters2, parameters3);

Example

#include <iostream>
using namespace std;

int sum(int a, int b) {
    return a + b;
}

float sum(float a, float b) {
    return a + b;
}

int main() {
    cout << sum(2, 3) << endl;
    cout << sum(2.5f, 3.5f) << endl;
    return 0;
}

Output

5
6

Explanation

In the above example, we have defined two functions with the same name "sum" but with different parameter types: one takes two integers, and the other takes two floats. Depending on the arguments passed, the appropriate function is called.

Use

Function overloading is useful when you want to reuse a function name but with slightly different functionality based on the data type or number of arguments passed.

Virtual Functions

Virtual functions are another way to achieve polymorphism in C++. In virtual functions, the function definition is provided in the base class, and it can be overridden by any derived class. This allows the derived classes to have different implementations of the same function.

Syntax

virtual return_type function_name(parameters) {
    // code
}

Example

#include <iostream>
using namespace std;

class Shape {
    public:
        virtual float getArea() {
            return 0;
        }
};

class Rectangle : public Shape {
    private:
        float length;
        float breadth;
    public:
        Rectangle(float l, float b) {
            length = l;
            breadth = b;
        }
        float getArea() {
            return length * breadth;
        }
};

class Circle : public Shape {
    private:
        float radius;
    public:
        Circle(float r) {
            radius = r;
        }
        float getArea() {
            return 3.14f * radius * radius;
        }
};

int main() {
    Shape* shape;
    Rectangle rectangle(10, 5);
    Circle circle(5);
    shape = &rectangle;
    cout << "Rectangle area: " << shape->getArea() << endl;
    shape = &circle;
    cout << "Circle area: " << shape->getArea() << endl;
    return 0;
}

Output

Rectangle area: 50
Circle area: 78.5

Explanation

In the above example, we have defined a base class "Shape" that has a virtual function "getArea()" which returns 0 (we need to define it as 0 because it's a pure virtual function). We then define two derived classes "Rectangle" and "Circle" which override the "getArea()" function with their own implementations. Then, we create an object of each derived class and assign it to a pointer of the base class type. We call the "getArea()" function on each object through the base class pointer, which calls the appropriate derived class implementation.

Use

Virtual functions are useful when you want to have a common function name in the base class but with different implementations in the derived classes. It allows for better code organization and maintenance, as well as the ability to switch implementations at runtime.

Important Points

  • Polymorphism is the ability of an object to take on many forms.
  • Function overloading allows for reusing a function name with different parameter types.
  • Virtual functions allow for having a common function name in the base class with different implementations in the derived classes.
  • A virtual function is declared with the "virtual" keyword and is defined in the base class.
  • Virtual functions can be overridden in any derived class.

Summary

In summary, polymorphism is a core feature of object-oriented programming languages that allows for code reuse and organization. In C++, there are two main types of polymorphism: function overloading and virtual functions. Function overloading allows for reusing a function name with different parameter types, while virtual functions allow for having a common function name in the base class with different implementations in the derived classes.

Published on: