OOP-Unit 3
OOP-Unit 3
Polymorphism
UNIT – 3: Polymorphism
Polymorphism is one of the most important concepts in OOP. It describes the ability of
something to have or to be displayed in more than one form. The different forms arise because
these entities can be assigned different meanings and used in various ways in multiple contexts.
Run time: Run time polymorphism is achieved when the object's method is invoked at
the run time instead of compile time. It is achieved by method overriding which is also known
as dynamic binding or late binding.
2
UNIT – 3: Polymorphism
The function to be invoked is known at the The function to be invoked is known at the
compile time. run time.
It is less flexible as mainly all the things It is more flexible as all the things execute at
execute at the compile time. the run time.
Function Overloading
Function Overloading is defined as the process of having two or more function with the same
name, but different in parameters is known as function overloading. In function overloading,
the function is redefined by using either different types of arguments or a different number of
arguments. It is only through these differences compiler can differentiate between the
functions.
Function overloading is performing within single class only. You cannot overload function
declarations that differ only by return type.
The advantage of Function overloading is that it increases the readability of the program
because you don't need to use different names for the same action.
3
UNIT – 3: Polymorphism
Example:
#include<iostream>
using namespace std;
class sum
{
public:
void addition(int x, int y)
{
cout<<x+y;
}
4
UNIT – 3: Polymorphism
int main()
{
sum s1;
s1.addition(10,20.50);
s1.addition(10,15);
s1.addition(10,5,7);
return 0;
}
Operator Overloading
Operator overloading is one of the best features of C++. By overloading the operators,
we can give additional meaning to the operators like +-*/=.,= etc., which by default are
supposed to work only on standard data types like int, float, char, void etc. It is an essential
concept in C++. It’s a type of polymorphism in which an operator is overloaded to give it the
user-defined meaning. For example '+' operator can be overloaded to perform addition on various
data types, like for Integer, String(concatenation) etc.
5
UNIT – 3: Polymorphism
Almost any operator can be overloaded in C++. However there are few operators which cannot
be overloaded. Operators that are not overloaded are follows
scope operator( :: ) =
sizeof =
member selector( . ) =
member pointer selector( * ) =
ternary operator(?:)=
1. Member Function
2. Non-Member Function
3. Friend Function
6
UNIT – 3: Polymorphism
Operator overloading function can be a member function if the Left operand is an Object of
that class, but if the Left operand is different, then Operator overloading function must be a
non-member function.
Operator overloading function can be made friend function if it needs access to the private and
protected members of class.
#include <iostream>
class Complex {
private:
int x, y;
public:
Complex() {}
Complex(int a, int b)
{
x = a;
y = b;
}
Complex operator+(Complex obj)
{
Complex res;
res.x = x + obj.x;
7
UNIT – 3: Polymorphism
res.y = y + obj.y;
return res;
}
void print()
{
cout <<"X = "<< x <<endl<<"Y = "<< y << endl;
}
};
int main()
{
Complex c1(10, 5);
Complex c2(2, 4);
You can also overload Relational operator like == , != , >= , <= etc. to compare two user-defined
object.
Example
class time
{
int hr,min,sec;
public:
time()
{
hr=0, min=0; sec=0;
}
8
UNIT – 3: Polymorphism
9
UNIT – 3: Polymorphism
Note :
One of the key features of class inheritance is that a pointer to a derived class is type-compatible with
a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and
versatile feature.
For example, X is a base class and Y is a derived class. The pointer pointing to X can also point to Y.
Class X
{ //Class body };
Class Y : public X
{ //class body };
void main(){
X *ptr; //pointer to base class
X x;
Y y;
ptr = &x; // Pointing to base class
ptr = &y; //Pointing to Derived class using base class pointer
}
Overriding
If we inherit a class into the derived class and provide a definition for one of the base class's
function again inside the derived class, then that function is said to be overridden, and this
mechanism is called Function Overriding
10
UNIT – 3: Polymorphism
Connecting the function call to the function body is called Binding. When it is done before the
program is run, its called Early Binding or Static Binding or Compile-time Binding.
Example:
class Base
{
public:
void shaow()
{
cout << "Base class\t";
}
};
int mian()
{
Base b; //Base class object
Derived d; //Derived class object
b.show(); //Early Binding Occurs
d.show(); //Early Binding Occurs
}
Output:
Base class Derived class
In the above example, we are calling the overrided function using Base class and Derived class
object. Base class object will call base version of the function and derived class's object will call
the derived version of the function.
11
UNIT – 3: Polymorphism
When we use a Base class's pointer or reference to hold Derived class's object, then Function
call Binding gives some unexpected results.
class Base
{
public:
void show()
{
cout << "Base class";
}
};
int main()
{
Base *b; //Base class pointer
Derived d; //Derived class object
b = &d; // Bind Base class pointer object with derived class object
b->show(); //Early Binding Occurs
}
Output:
Base class
In the above example, although, the object is of Derived class, still Base class's method is called.
This happens due to Early Binding.
Compiler on seeing Base class's pointer, set call to Base class's show() function, without
knowing the actual object type.
12
UNIT – 3: Polymorphism
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Early Binding Occurs
}
Output:
Base class
When we use Base class's pointer to hold Derived class's object, base class pointer or
reference will always call the base version of the function
13
UNIT – 3: Polymorphism
class Base
{
public:
virtual void show()
{
cout << "Base class";
}
};
int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Occurs
}
Output:
Derived class
On using Virtual keyword with Base class's function, Late Binding takes place and the
derived version of function will be called, because base class pointer pointes to Derived class
object.
14
UNIT – 3: Polymorphism
1. Only the Base class Method's declaration needs the Virtual Keyword, not the definition.
2. If a function is declared as virtual in the base class, it will be virtual in all its derived classes.
A pure virtual function is a function that has the notation "= 0" in the declaration of that
function. Why we would want a pure virtual function and what a pure virtual function looks like
is explored in more detail below.
The "= 0" portion of a pure virtual function is also known as the pure specifier, because it’s
what makes a pure virtual function “pure”. Although the pure specifier appended to the end of
the virtual function definition may look like the function is being assigned a value of 0, that
is not true. The notation "= 0" is just there to indicate that the virtual function is a pure virtual
function, and that the function has no body or definition.
Any class that has at least one pure virtual function is called an abstract class. This means
that in our example above, the SomeClass class is an abstract class. An abstract
class cannot have an instance of itself created. So, using our example class from above, the
following code would not work:
This also means that any class that derives from an abstract class must override the
definition of the pure virtual function in the base class, and if it doesn’t then the derived class
becomes an abstract class as well.
15
UNIT – 3: Polymorphism
16