OOPM POLYMORPHISM
OOPM POLYMORPHISM
The word “polymorphism” means having many forms. In simple words, we can define polymorphism as
the ability of a message to be displayed in more than one form. A real-life example of polymorphism is a
person who at the same time can have different characteristics. A man at the same time is a father, a
husband, and an employee. So the same person exhibits different behavior in different situations. This is
called polymorphism.
3.Method overriding allows a sub class to use all the general definitions that a super class
provides and add specialized definitions through overridden methods.
4.Method overriding works together with inheritance to enable code reuse of existing classes
without the need for re-compilation.
Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.
a. Function Overloading
When there are multiple functions with the same name but different parameters, then the functions are
said to be overloaded, hence this is known as Function Overloading. Functions can be overloaded
by changing the number of arguments or/and changing the type of arguments. In simple terms, it is a
feature of object-oriented programming providing many functions that have the same name but distinct
parameters when numerous tasks are listed under one function name. There are certain Rules of Function
Overloading that should be followed while overloading a function.
Syntax
class class_Name
{
Returntype method()
{
...........
...........
}
Returntype method(datatype1 variable1)
{
...........
...........
}
Returntype method(datatype1 variable1, datatype2 variable2)
{
...........
...........
}
};
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
Out
put
30
60
In this example, we have created two overloaded methods that differs in data type. The first sum
method receives two integer arguments and second sum method receives two float arguments.
#include<iostream.h>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(float a, float b)
{
cout<<a+b+c;
}
};
void main()
{
clrscr();
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30);
}
b.Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this ability
is known as operator overloading. For example, we can make use of the addition operator (+)
for string class to concatenate two strings. We know that the task of this operator is to add two
operands. So a single operator ‘+’, when placed between integer operands, adds them and
when placed between string operands, concatenates them.
Operator overloading is an important concept in C++. It is a type of polymorphism in which
an operator is overloaded to give user defined meaning to it. Overloaded operator is used to
perform operation on user- defined data type. For example '+' operator can be overloaded to
perform addition on various data types, like for Integer, String (concatenation) etc.
scope operator - ::
sizeof
member selector - .
member pointer selector - *
ternary operator - ?:
class Complex {
private:
public:
Complex(int r = 0, int i = 0)
real = r;
imag = i;
Complex res;
return res;
void print() { cout << real << " + i" << imag << '\n'; }
};
int main()
{
Complex c3 = c1 + c2;
c3.print();
2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding and dynamic
polymorphism are other names for runtime polymorphism. The function call is resolved at
runtime in runtime polymorphism. In contrast, with compile time polymorphism, the compiler
determines which function call to bind to the object after deducing it at runtime.
a. Function Overriding
Function Overriding occurs when a derived class has a definition for one of the member
functions of the base class. That base function is said to be overridden.
#include <iostream>
class baseClass {
public:
void print() {
};
public:
void print() {
}
};
//Driver code
int main() {
derivedClass d1;
d1.print();
return 0;
In Method Overloading, Methods of the In Method Overriding, sub classes have the
same class shares the same name but each same method with same name and exactly
Definition method must have different number of the same number and type of parameters and
parameters or parameters having different same return type as a super class.
types and order.
Method Overloading means more than Method Overriding means method of base
Meaning one method shares the same name in the class is re-defined in the derived class
class but having different signature. having same signature.
Method Overloading is to “add” or Method Overriding is to “Change” existing
Behavior
“extend” more to method’s behavior. behavior of method.
Method Overloading does not require Method Overriding requires at least two
No. of Classes more than one class for overloading. classes for overriding.