Computer Science Notes for Class 12 (ASSEB / AHSEC)

Chapter 2 - Polymorphism & Function Overloading

Polymorphism

Definition

The word Polymorphism comes from two Greek words :

  • Poly means Many
  • Morphs means Forms

So, Polymorphism means “one thing having many forms.”

In C++, polymorphism allows the same function name or same operator to behave differently in different situations.

Example

Think about the word “draw”

  • You can draw a circle
  • You can draw a rectangle
  • You can draw a triangle

The action name is the same → draw()
But the work performed is different.

This is the basic idea of polymorphism.

Types of Polymorphism

There are mainly two types of polymorphism

  • Compile-time Polymorphism – achieved at compile time
  • Run-time Polymorphism – achieved at runtime.

Compile-time (Static) Polymorphism

In compile time polymorphism, the compiler decides which function to call based on the number, type or order of the arguments.

It is also called Early Binding

It is achieved through:

  • Function Overloading
  • Operator Overloading

Run-time (Dynamic) Polymorphism

Run-time Polymorphism is achieved at runtime. It is also called Late Binding. It is achieved though :

  • Function Overiding
  • Virtual Functions

Here we will learn compile-time polymorphism in detail i.e.,Function Overloading

Function Overloading

Definition

Function Overloading is a feature of C++ that allows us to create multiple functions with the same name but different parameter lists. The functions may differ in the following ways:

  • No of parameters
  • Type of parameters
  • Order of parameters

Rules for Function Overloading

  • Function name should be same.
  • Return type could be same or different. It does not matter.
  • Parameter list should be different i.e., number, type and / or order of parameters should be different

Example

         void sum (int a, int b);                     Example – sum(2,3);
         void sum (float a , float b);               Example – sum(3.5, 5.5);
         void sum (int a, int b, int c);             Example – sum(2,3,4);

How function overloading implements Polymorphism?

Suppose we want to perform addition.

We may need:

  • addition of two integers
  • addition of three integers
  • addition of floating-point numbers

Instead of creating separate function names like:

sumInt()
sumFloat()
sumThree()

we create functions having the same name:

sum(int,int)
sum(int,int,int)
sum(float, float)

This is polymorphism because:

The same function name sum() performs different tasks depending on the arguments.

Program on Function Overloading

Program 1 — Overloading with different number and types of parameters

#include <iostream>
using namespace std;

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

int main() {
cout << sum(2, 3) << endl;                       // calls int version
cout << sum(2.5, 3.5) << endl;                   // calls float version
cout << sum(1, 2, 3);                                   // calls 3-parameter version
}

Quick Revision

Scroll to Top