Polymorph is m
Polymorph is m
The word “polymorphism” means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than
one form. A real-life example of polymorphism is a person who at the same
time can have different characteristics. A man at the same time is a father, a
husband, and an employee. So the same person exhibits different behaviour in
different situations. This is called polymorphism. Polymorphism is considered
one of the important features of Object-Oriented Programming.
Types of Polymorphism
● Compile-time Polymorphism
● Runtime Polymorphism
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading of operator
overloading.
A. Function Overloading
When there are multiple functions with the same name but different parameters,
then the functions are said to be overloaded, hence this is known as Function
Overloading. Functions can be overloaded by changing the number of
arguments or/and changing the type of arguments. In simple terms, it is a feature
of object-oriented programming providing many functions that have the same
name but distinct parameters when numerous tasks are listed under one function
name. There are certain Rules of Function Overloading that should be followed
while overloading a function.
// Driver code
int main()
{
Geeks obj1;
B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data
type, this ability is known as operator overloading. For example, we can make
use of the addition operator (+) for string class to concatenate two strings. We
know that the task of this operator is to add two operands. So a single operator
‘+’, when placed between integer operands, adds them and when placed
between string operands, concatenates them.
Syntax:
Return_Type classname :: operator op(Argument list)
{
Function Body
} // This can be done by declaring the function
Here,
● Return_Type is the value type to be returned to another object.
● operator op is the function where the operator is a keyword.
● op is the operator to be overloaded.
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1.display();
return 0;
}
should have only one argument and the unary should not have an
argument.
2. In the case of a friend function, the binary operator should have only
two arguments and the unary should have only one argument.
3. Operators that cannot be overloaded are .* :: ?:
class Distance {
public:
int feet, inch;
// Constructor to initialize
// the object's value
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Overloading(-) operator to
// perform decrement operation
// of Distance object
void operator-()
{
feet--;
inch--;
cout << "\nFeet & Inches(Decrement): " <<
feet << "'" << inch;
}
};
// Driver Code
int main()
{
Distance d1(8, 9);
class Number {
private:
int value;
public:
// Constructor
Number(int v) : value(v) {}
int main() {
Number num(10);
return 0;
}
class Number {
private:
int value;
public:
// Constructor
Number(int v) : value(v) {}
int main() {
Number num1(10), num2(20);
return 0;
}
class Number {
private:
int value;
public:
// Constructor
Number(int v) : value(v) {}
int main() {
Number num1(10), num2(20);
return 0;
}
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 calls to
bind to the object after deducing it at runtime.
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.
Using virtual functions in the base class ensures that the function can be
overridden in these cases.
Thus, virtual functions actually fall under function overriding. For example,
Call to Derived Class Function with the help of Base Class Pointer
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {
cout << "Base Function" << endl;
}
};
int main() {
Derived derived1;
return 0;
}
Call to Base Class Function with the help of Derived Class Object
#include <iostream>
using namespace std;
class Base {
public:
// Base class version of show function
void show() {
cout << "Base class show() called" << endl;
}
};
int main() {
Derived obj;
// Calling the base class's show function using the derived object
obj.Base::show();
return 0;
}