Polymorphism (1) 1
Polymorphism (1) 1
Polymorphism (1) 1
Polymorphism
Types Of Polymorphism
Static Polymorphism(compile Time)
Function Overloading
Operator Overloading
Dynamic Polymorphism(run Time)
Virtual Function
Pure Virtual Function
Types of Polymorphism
Compile Time Polymorphism Run time Polymorphism
In Compile time Polymorphism, the call is In Run time Polymorphism, the call is not
resolved by the compiler. resolved by the compiler.
It is also known as Static binding, Early It is also known as Dynamic binding, Late
binding . binding .
It provides fast execution because the method It provides slow execution as compare to
that needs to be executed is known early at early binding because the method that needs
the compile time. to be executed is known at the runtime.
Compile time polymorphism is less flexible Run time polymorphism is more flexible as
as all things execute at compile time. all things execute at run time.
• In C++, two functions can have the same name if the number and/or type
of arguments passed is different.
• These functions having the same name but different arguments are known
as overloaded functions.
Example 1: Overloading Using Different Types of Parameter
#include <iostream>
using namespace std;
// function with float type parameter
float absolute(float var)
{
if (var < 0.0)
var = -var;
return var;
}
// function with int type parameter
int absolute(int var)
{
if (var < 0)
var = -var;
return var;
}
continued
int main()
{
// call function with int type parameter
cout << "Absolute value of -5 = " << absolute(-5) << endl;
// call function with float type parameter
cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
}
Example 2: Overloading Using Different Number of Parameters
#include <iostream>
using namespace std;
int main()
{
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;
}
Operator Overloading
Operator overloading is basically function overloading, where different
operator functions have the same symbol but different operands.
Syntax:
class className
{
public
returnType operator symbol (arguments)
{
//code to be executed
}
};
• returnType - is the return type of the function.
• operator - is a keyword.
• symbol - is the operator we want to overload. Like: +, <, -, ++, etc.
• arguments - is the arguments passed to the function.
Example1: ++ Operator (Unary Operator) Overloading
#include <iostream>
using namespace std;
class Count
{
private: int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ ()
{
value = value + 1;
}
void display()
{
cout << "Count: " << value << endl;
}
};
continued
int main()
{
Count count1;
// Call the "void operator++ ()" function
++count1;
count1.display();
return 0;
}
Example 2: ++ Operator (Unary Operator) Overloading
// Overload ++ when used as prefix and postfix
#include <iostream>
using namespace std;
class Count
{
private: int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ ()
{
++value;
}
// Overload ++ when used as postfix
void operator ++ (int)
{
value++;
}
continued
void display()
{
cout << "Count: " << value << endl;
}
};
int main()
{
Count count1;
// Call the "void operator ++ (int)" function
count1++;
count1.display();
// Call the "void operator ++ ()" function
++count1;
count1.display();
return 0;
}
There are 4 operators that cannot be overloaded in C++.
They are:
:: (scope resolution)
. (member selection)
.* (member selection through pointer to function)
?: (ternary operator)
Virtual functions
Virtual functions are used to achieve Dynamic polymorphism
(run time polymorphism)
16
Rules for Virtual Functions:
20
Pure Virtual functions
#include <iostream>
using namespace std;
class Base
{
public:
virtual void show() = 0;
};
class Derived : public Base
{
public:
void show()
{
cout << "Derived class is derived from the base class.";
}
};
int main()
{
Base *b;
Derived d;
b = &d;
b->show();
return 0;
}
EXERCISE
PROGRAMS