C++ Object Oriented Programming
Adnan Sher
Department of Information Technology
University of Haripur
Polymorphism
Flow of Lecture
Example:
Types of Compile-time Operator Example: Run-time Function Virtual Why
Introduction Overload Comparison
Polymorphism Polymorphism Overloading Overloading ++ Polymorphism Overriding Function Polymorphism
sum()
3
Department of Information Technology – The University of Haripur
Introduction
• Polymorphism is an important concept of OOP. It simply means more
than one form. That is, the same entity (function or operator) behaves
differently in different scenarios.
• For example: The + operator in C++ is used to perform two specific
functions. When it is used with numbers (integers and floating-point
numbers), it performs addition.
int a = 5;
int b = 6;
int sum = a + b; // sum = 11
4
Department of Information Technology – The University of Haripur
And when we use the + operator with strings, it
performs string concatenation. For example,
string firstName = "abc ";
string lastName = "xyz";
// name = "abc xyz"
string name = firstName + lastName;
5
Department of Information Technology – The University of Haripur
We can implement polymorphism in
C++ using the following ways:
Function Operator Function Virtual
overloading overloading overriding functions
6
Department of Information Technology – The University of Haripur
In C++ polymorphism is mainly divided
into two types:
Polymorphism
Compile Time Runtime
Function Overloading Virtual Functions
Operator Overloading
7
Department of Information Technology – The University of Haripur
Compile Time Polymorphism
This type of polymorphism is achieved by
function overloading or operator
overloading.
8
Department of Information Technology – The University of Haripur
C++ Function Overloading
In C++, we can use two functions having the same name if
they have different parameters (either types or number of
arguments).
And, depending upon the number/type of arguments,
different functions are called.
9
Department of Information Technology – The University of Haripur
Example: Overload sum() Function
int sum(int num1, int num2) { int main() {
return num1 + num2; // Call function with 2 int parameters
} cout << "Sum 1 = " << sum(5, 6) << endl;
double sum(double num1, double num2) { // Call function with 2 double parameters
return num1 + num2; cout << "Sum 2 = " << sum(5.5, 6.6) << endl;
}
// Call function with 3 int parameters
cout << "Sum 3 = " << sum(5, 6, 7) << endl;
int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
return 0;
} }
10
Department of Information Technology – The University of Haripur
Cont.,
Here, we have created 3 different sum() functions with different parameters
(number/type of parameters). And, based on the arguments passed during a
function call, a particular sum() is called.
It's a compile-time polymorphism because the compiler knows which function to
execute before the program is compiled.
11
Department of Information Technology – The University of Haripur
C++ Operator Overloading
In C++, we can overload an operator if we are operating on user-defined
types like objects or structures.
We cannot use operator overloading for basic types such as int, double,
etc.
Operator overloading is basically function overloading, where different
operator functions have the same symbol but different operands.
And, depending on the operands, different operator functions are
executed.
12
Department of Information Technology – The University of Haripur
Example:
class Complex{ Complex operator +(const Complex &C)
private: {
int real; Complex ans;
int img; ans.real=real+C.real;
public: ans.img=img+C.img;
Complex() { return ans;
real = 0; }
img = 0; };
} int main() {
Complex(int real, int img) Complex C1(3,4);
{ Complex C2(5,6);
this->real=real;
this->img=img; Complex C3=C1+C2;
}
void display() { C3.display();
cout << real << " + i" << img << endl; return 0;
} }
13
Department of Information Technology – The University of Haripur
Runtime Polymorphism
This type of polymorphism is
achieved by Function Overriding.
14
Department of Information Technology – The University of Haripur
C++ Function Overriding
In C++ inheritance, we can have the same function in the base class as well
as its derived classes.
When we call the function using an object of the derived class, the function
of the derived class is executed instead of the one in the base class.
So, different functions are executed depending on the object calling the
function.
This is known as function overriding in C++.
15
Department of Information Technology – The University of Haripur
Example: Function Overriding
class Base { int main() {
public:
virtual void print() { Derived derived1;
cout << "Base Function" << endl;
}
}; // Call print() function of
Derived class
class Derived : public Base { derived1.print();
public:
void print() {
cout << "Derived Function" << endl; return 0;
}
}; }
16
Department of Information Technology – The University of Haripur
Explanation
Here, we have used a print() function in the Base class and the
same function in the Derived class
When we call print() using the Derived object derived1, it
overrides the print() function of Base by executing the print()
function of the Derived class.
It's a runtime polymorphism because the function call is not
resolved by the compiler, but it is resolved in the runtime instead.
17
Department of Information Technology – The University of Haripur
C++ Virtual Functions
In C++, we may not be able to override functions if we use a
pointer of the base class to point to an object of the derived class.
Using virtual functions in the base class ensures that the function
can be overridden in these cases.
Thus, virtual functions fall under function overriding.
18
Department of Information Technology – The University of Haripur
Example: Use of Virtual Functions
class Base { class Derived : public Base {
public: public:
virtual void print() { void print() {
cout << "Base Function" << cout << "Derived Function"
endl; << endl;
} }
}; };
19
Department of Information Technology – The University of Haripur
Cont.,
int main() {
Derived derived1;
// pointer of Base type that points to derived1
Base* base1 = &derived1;
// calls member function of Derived class
base1->print();
return 0;
}
20
Department of Information Technology – The University of Haripur
Example Explanation
Here, we have used a virtual function print() in the Base
class to ensure that it is overridden by the function in the
Derived class.
Virtual functions are runtime polymorphism.
21
Department of Information Technology – The University of Haripur
Why Polymorphism?
Polymorphism
allows us to create
consistent code.
For example: Suppose we need to calculate the area of
a circle and a square. To do so, we can create a Shape
class and derive two classes Circle and Square from it.
In this case, it makes sense to create a function having the same name
calculateArea() in both the derived classes rather than creating
functions with different names, thus making our code more consistent.
22
Department of Information Technology – The University of Haripur
Comparison
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 run time.
time.
It is also known as overloading, early binding and static It is also known as overriding, Dynamic binding and late
binding. binding.
Overloading is a compile time polymorphism where Overriding is a run time polymorphism where more than
more than one method is having the same name but one method is having the same name, number of
with the different number of parameters or the type of parameters and the type of the parameters.
the parameters.
It is achieved by function overloading and operator It is achieved by virtual functions and pointers.
overloading.
It provides fast execution as it is known at the compile It provides slow execution as it is known at the run time.
time.
It is less flexible as mainly all the things execute at the It is more flexible as all the things execute at the run
compile time. time.
23
Department of Information Technology – The University of Haripur