4 polymorphism
4 polymorphism
int a = 6;
int b = 6;
int sum = a + b; //
sum =12
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 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
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
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.
#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
Another example:
#include <iostream>
using namespace std;
B. Operator Overloading
int plusFunc(int x, int 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.
Unary operators
Binary operators
Special operators ( [ ], (), etc)
Where the return type is the type of value returned by the function.
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:
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.
#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
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.