0% found this document useful (0 votes)
0 views

4 polymorphism

Polymorphism, derived from the Greek words for 'many forms', refers to the ability of a message to be displayed in multiple forms, particularly in Object-Oriented Programming. It can be categorized into compile-time polymorphism, achieved through function and operator overloading, and runtime polymorphism, achieved through function overriding and virtual functions. Each type has distinct characteristics, such as compile-time being resolved during compilation and runtime being resolved during execution.

Uploaded by

Sonu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

4 polymorphism

Polymorphism, derived from the Greek words for 'many forms', refers to the ability of a message to be displayed in multiple forms, particularly in Object-Oriented Programming. It can be categorized into compile-time polymorphism, achieved through function and operator overloading, and runtime polymorphism, achieved through function overriding and virtual functions. Each type has distinct characteristics, such as compile-time being resolved during compilation and runtime being resolved during execution.

Uploaded by

Sonu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Polymorphism

The term "Polymorphism" is the combination of "poly" + "morphs"


which means many forms. It is a greek word. In simple words, we can
define polymorphism as the ability of a message to be displayed in
more than one form.

Real Life Example Of Polymorphism

Let's consider a real-life example of polymorphism. A lady behaves


like a teacher in a classroom, mother or daughter in a home and
customer in a market. Here, a single person is behaving differently
according to the situations. Polymorphism is considered one of the
important features of Object-Oriented Programming.

Consider this example:


The “ +” operator in c++ can perform two specific functions at two
different scenarios i.e when the “+” operator is used in numbers, it
performs addition.

int a = 6;
int b = 6;
int sum = a + b; //
sum =12

And the same “+” operator is used in the string, it performs


concatenation.
string firstName = "Great ";
string lastName = "Learning";
// name = "Great Learning "
string name = firstName + lastName;

There are two types of polymorphism in C++:


 Compile-time Polymorphism
 Runtime Polymorphism
Differences b/w compile time and run time polymorphism.
Compile time polymorphism Run time polymorphism

The function to be invoked is known at the compile The function to be invoked is known at the
time. run time.

It is also known as overloading, early binding and It is also known as overriding, Dynamic
static binding. binding and late binding.

Overloading is a compile time polymorphism where Overriding is a run time polymorphism where
more than one method is having the same name but more than one method is having the same
with the different number of parameters or the type name, number of parameters and the type of
of the parameters. the parameters.

It is achieved by function overloading and operator It is achieved by virtual functions and


overloading. pointers.

It provides fast execution as it is known at the It provides slow execution as it is known at


compile time. the run time.

It is less flexible as mainly all the things execute at It is more flexible as all the things execute at
the compile time. the run time.
1. Compile-Time Polymorphism

In compile-time polymorphism, a function is called at the time of


program compilation. We call this type of polymorphism as early
binding or Static binding. This type of polymorphism is achieved by
function overloading or operator overloading .

Overloading
If we Create two or more members that have the same name but having
different type of parameter is known as "Overloading" .

a. Function Overloading

Function overloading means one function can perform many tasks. In


C++, a single function is used to perform many tasks with the same
name and different types of arguments. In the function overloading
function will call at the time of program compilation. It is an example
of compile-time polymorphism. If multiple functions having same
name but parameters of the functions should be different is known as
Function Overloading.
If we have to perform only one operation and having same name

of the functions increases the readability of the program.


Suppose you have to perform addition of the given numbers but there
can be any number of arguments, if you write the function such as
a(int,int) for two parameters, and b(int,int,int) for three parameters
then it may be difficult for you to understand the behavior of the
function because its name differs.
The parameters should follow any one or more than one of the
following conditions for Function overloading:
 Parameters should have a different type

add(int a, int b)
add(double a, double b)
Why is function overloading in C++ is used?
Function overloading is similar to polymorphism that helps us to get
different behaviour, with the same name of the function. Function
overloading in c++ is used for code reusability and to save memory.

Rules of Function Overloading in C++


1. These functions have different parameter type
sum(int a, int b)
sum(double a, double b)

2. These functions have a different number of parameters


sum(int a, int b)
sum(int a, int b, int c)
3. These functions have a different sequence of parameters
sum(int a, double b)
sum(double a, int b)
The above three cases are valid cases of overloading. We can have
any number of functions, but remember that the parameter list must
be different. Below is the implementation of the above discussion:

#include <iostream>
using namespace std;
void add(int a, int b){
cout << "sum = " << (a + b);
}
void add(double a, double b){
cout << endl << "sum = " << (a + b);
}
int main(){
add(10, 2);
add(5.3, 6.2);
return 0;
}
In the example, we overload the add function to work for
both int and double:
12

11.5

In the above example, we use function ADD() to perform many tasks


which is a property of polymorphism in C++.

Another example:

#include <iostream>
using namespace std;

B. Operator Overloading
int plusFunc(int x, int y) {
return x + y;
}

double plusFunc(double x, double y) {


return x + y;
}

int main() {
int myNum1 = plusFunc(8, 5);
double myNum2 = plusFunc(4.3, 6.26);
cout << "Int: " << myNum1 << "\n";
cout << "Double: " << myNum2;
return 0;
}
Output:

Int 13
Double 10.56

b. Operator Overloading
it can add special features to the functionality and behaviour of already
existing operators like arthematic and other operations. The mechanism
of giving special meaning to an operator is known as operator
overloading. For example, we can overload an operator ‘+’ in a class-
like string to concatenate two strings by just using +.
The purpose of operator overloading is to provide a special meaning
to the user-defined data types.

The advantage of Operators overloading is to perform different


operations on the same operand.

Operators that can be overloaded

 Unary operators
 Binary operators
 Special operators ( [ ], (), etc)

There are some C++ operators which we can't overload.


 Class member access operator (. (dot), .* (dot-asterisk))
 Scope resolution operator (::)
 Conditional Operator (?:)
 Size Operator (sizeof)

What are the rules for operator overloading in C++?


o Existing operators can only be overloaded, but the new operators
cannot be overloaded.
o The overloaded operator contains atleast one operand of the user-
defined data type.
o We cannot use friend function to overload certain operators.
However, the member function can be used to overload those
operators.
o When unary operators are overloaded through a member function
take no explicit arguments, but, if they are overloaded by a friend
function, takes one argument.
o When binary operators are overloaded through a member
function takes one explicit argument, and if they are overloaded
through a friend function takes two explicit arguments.

Syntax of Operator Overloading

1. return_type class_name : : operator op(argument_list)


2. {
3. // body of the function.
4. }

Where the return type is the type of value returned by the function.

class_name is the name of the class.

operator op is an operator function where op is the operator being


overloaded, and the operator is the keyword.
// program to overload the binary operators.
#include <iostream>
using namespace std;
class A
{

int x;
public:
A(){}
A(int i)
{
x=i;
}
void operator+(A);
void display();
};

void A :: operator+(A a)
{

int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;

}
int main()
{
A a1(5);
A a2(4);
a1+a2;
return 0;
}

Output:

The result of the addition of two objects is : 9


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 in C++ is termed as the redefinition of base
class function in its derived class with the same signature i.e. return
type and parameters. The derived classes inherit features of the base
class.
Suppose, the same function is defined in both the derived class and the
based class. Now if we call this function using the object of the derived
class, the function of the derived class is executed.

This is known as function overriding in C++. The function in derived


class overrides the function in base class.

Real-Life Example of Function Overriding

The relationship between RBI(The Reserve Bank of India) and Other


state banks like SBI, PNB, ICICI, etc. Where the RBI passes the same
regulatory function and others follow it as it is.
Function Overloading Function Overriding

It falls under Compile-Time It falls under Runtime


polymorphism Polymorphism

A function can be overloaded A function cannot be overridden


multiple times as it is resolved at multiple times as it is resolved at
Compile time Run time

Can be executed without Cannot be executed without


inheritance inheritance

They are in the same scope They are of different scopes.

// C++ program to demonstrate function overriding

#include <iostream>
using namespace std;
Output: Derived Function
Here, the same function print() is defined in
both Base and Derived classes.
So, when we call print() from the Derived object derived1,
the print() from Derived is executed by overriding the function
in Base.
B. Virtual Function

A virtual function is a member function that is declared in the base


class using the keyword virtual and is re-defined (Overridden) in the
derived class.
Some Key Points About Virtual Functions:
 Virtual functions are Dynamic in nature.
 They are defined by inserting the keyword “virtual” inside a
base class and are always declared with a base class and
overridden in a child class
 A virtual function is called during Runtime

If it is necessary to use a single pointer to refer to all the different


classes’ objects. This is because we will have to create a pointer to the
base class that refers to all the derived objects.

But, when the base class pointer contains the derived class address, the
object always executes the base class function. For resolving this
problem, we use the virtual function.

You might also like