Polymorphism:
Polymorphism is the ability of a message to be displayed in more than one form.
Polymorphism is the combination of “poly”+”morphism” which means many forms.
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 behavior in different situations. This is called polymorphism.
Advantages Of Polymorphism
i) It helps the programmer to reuse the code and classes once written called code reusability.
- ii) A single function name can be used to store the multiple data types.
iii) It used to extend the functionality of class.
Types of Polymorphism
1) Compile-time Polymorphism
2) Runtime Polymorphism
- Compile-Time Polymorphism
The polymorphism which can be achieved by function overloading or operator overloading at a compile time is called compile time polymorphism.
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. In other words,The process of having two or more function with the same name but different parameters is known as function overloading.Function can be overloaded by changing the number of arguments or by changing the type of the arguments.
Syntax:
Void fun(){}
Void fun(int , int){}
Example 1:
#include <iostream>
class Add {
public:
int sum(int num1, int num2) {
return num1 + num2;
}
int sum(int num1, int num2, int num3) {
return num1 + num2 + num3;
}
};
int main() {
Add obj;
cout << “Output: ” << obj.sum(10, 20) << “\n”;
cout << “Output: ” << obj.sum(11, 22, 33) << “\n”;
return 0;
}
Example2:
#include<iostream>
using namespace std;
class Geeks {
public:
void func(int x)
{
cout << “value of x is ” << x << endl;
}
void func(double x)
{
cout << “value of x is ” << x << endl;
}
void func(int x, int y)
{
cout << “value of x and y is ” << x << “, ” << y<< endl;
}
};
int main()
{
Geeks obj1;
obj1.func(7);
obj1.func(9.132);
obj1.func(85, 64);
return 0;
}
Operator Overloading
Operator overloading is a compile time polymorphism in which the operator is overloaded to provide the special meaning to the user defined data type. C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.
The advantage of the operator overloading is to perform the different operation on the same operator.
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.
Example:
#include<iostream>
using namespace std;
int main(){
int sum=25+35;//used for addition
cout<<“addition”<<sum;
string s1=”we are members”;
string s2=”of Nexu cornerl”;
string s3=s1 +s2;
cout<< s3<<endl;
return 0;
}
We can overload the C++ operators except the following operators.
1) Scope resolution operator (::)
2) Dot operator (.)
3) Size of the operator ()
4) Ternary operator (? 🙂
5) Pointer to member selection operator (.*)
Declaration of operator overloading
Syntax:
Return_type/class_name operator symbol (argument_list)
{
//body of statements
}
Types of operator overloading
1) Unary operator overloading
2) Binary operator overloading
1) Unary operator overloading
An operator which contains only one operand is called unary operator overloading.
The increment operator ++ and decrement operator — are examples of unary operators.
Example to Overload ++ when used as prefix:
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() {
Value=5;
}
void operator ++ () {
++value;
}
void display() {
cout << “Count: ” << value << endl;
}
};
int main() {
Count count1;
Count1.display();
++count1;
count1.display();
return 0;
}
Write a C++ program to show – -operator overloading
#include <iostream>
using namespace std;
class Distance {
public:
int feet, inch;
Distance(int f, int i)
{
feet = f;
inch = i;
}
void operator–()
{
feet–;
inch–;
cout << “\nFeet & Inches(Decrement): ” << feet << “‘” << inch;
}
};
int main()
{
Distance d1(8, 9);
–d1;
return 0;
}
2) Binary operator overloading
An operator which contain two operands is called binary operator overloading.Arithmetic ,rational and logical are the example of the binary operator.
Program to Demonstrate Binary Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r , int i)
{
real = r;
imag = i;
}
Complex operator + (Complex obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() {
cout << real << ” + i” << imag << ‘\n’;
}
};
int main()
{
Complex c1(10, 5), c2(2, 4);
c3 = c1 + c2;
c3.print();
return0;
}
Example 2: C++ program to show binary operator overloading
#include <iostream>
using namespace std;
class Distance {
public:
int feet, inch;
Distance()
{
feet = 0;
inch = 0;
}
Distance(int f, int i)
{
feet = f;
inch = i;
}
Distance operator+(Distance& d2)
{
Distance d3;
d3.feet = feet + d2.feet;
d3.inch = inch + d2.inch;
return d3;
}
};
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
d3 = d1 + d2;
cout << “\nTotal Feet & Inches: ” << d3.feet << “ ‘ “ << d3.inch;
return 0;
}
Example: Overloading operator to add objects
#include<iostream>
using namespace std;
class Complex
{
int x, y;
public:
void input()
{
cout << ” Input two complex number: ” << endl;
cin >> x >> y;
}
Complex operator + (Complex obj)
{
Complex A;
A.x = x + obj.x;
A.y = y + obj.y;
return (A);
}
void print()
{
cout << x << ” + ” << y << “i” << “\n”;
}
int main ()
{
Complex x1, y1, sum;
y1.input();
sum = x1 + y1;
return 0;
}
2) Run-time Polymorphism
The polymorphism which can be achieved by Function Overriding and virtual function at the run time is called the run time polymorphism. Late binding and dynamic polymorphism are other names for runtime polymorphism .The function call is resolved at runtime in runtime polymorphism.
Function Overriding
When the base class and derived class have same member function with same name and same parameters then it is called function overriding. It occurs when a derived class has a definition for one of the member functions of the base class.
C++ program to demonstrate function overriding by calling the overridden function of a member function from the child class
#include <iostream>
using namespace std;
class Parent {
public:
void NepalEnotes_Print()
{
cout << “Base Function” << endl;
}
};
class Child : public Parent {
public:
void NepalEnotes_Print()
{
cout << “Derived Function” << endl;
Parent::NepalEnotes_Print();
}
};
int main()
{
Child Child_Derived;
Child_Derived.NepalEnotes_Print();
return 0;
}
Example2: Function Overriding
#include <iostream>
using namespace std;
class Car
{
public: void Start()
{
cout << “Car Started” << endl; }
void Stop()
{
cout << “Car Stopped” << endl;
}
};
class Innova:public Car
{
public: void Start()
{
cout << “Innova Started” << endl;
}
void Stop()
{
cout << “Innova Stopped” << endl;
}
};
class Swift:public Car
{
public: void Start()
{
cout << “Swift Started” << endl;
}
void Stop()
{
cout << “Swift Stopped” << endl;
}
int main()
{
Swift s;
s.start();
s.stop();
return 0;
}
Virtual Function
A virtual function is a member function that is declared in the base class using the keyword virtual and is re-defined (Overridden) in the derived class.
Some Key Points about Virtual Functions:
1) Virtual functions are Dynamic in nature.
2) They are defined by inserting the keyword “virtual” inside a base class and are always declared with a base class and overridden in a child class
3) A virtual function is called during Runtime.
4) It is used to achieve run time polymorphism.
C++ Program to demonstrate the Virtual Function
#include <iostream>
using namespace std;
class GFG_Base {
public:
// virtual function
virtual void display()
{
cout << “Called virtual Base Class function”<< “\n\n”;
}
void print()
{
cout << “Called GFG_Base print function”<< “\n\n”;
}
};
class GFG_Child : public GFG_Base {
public:
void display()
{
cout << “Called GFG_Child Display Function”<< “\n\n”;
}
void print()
{
cout << “Called GFG_Child print Function“<< “\n\n”;
}
};
int main()
{
GFG_Base* base;
GFG_Child child;
base = &child;
base->display();
base->print();
}
Example2: Virtual function
#include <iostream>
using namespace std;
class Car {
public: virtual void Start()
{
cout << “Car Started” << endl;
}
virtual void Stop()
{
cout << “Car Stopped” << endl;
}
};
class Innova:public Car
{
public: void Start()
{
cout << “Innova Started” << endl;
}
void Stop()
{
cout << “Innova Stopped” << endl;
}
};
class Swift:public Car
{
public: void Start()
{
cout << “Swift Started” << endl;
} void Stop()
{
cout << “Swift Stopped” << endl; }
};
int main() {
Car *c;
c->Start();
c->Stop();
Car=&Innova;
c->Start();
c->Stop();
Car=&swift;
c->Start();
c->Stop();
return 0;
}
Pure virtual function
Pure virtual function are the virtual function which has no definition or implementation.
They start with “virtual” keyword and declared by assigning 0.A pure virtual function is implemented by classes which are derived from abstract class.
Syntax:
Virtual return_type function_name()=0;
Abstract classes
A class which contains at least one pure virtual function. An abstract class is a class is designed to specifically used as base class. No any object of abstract class can be created. It allows base class to provide only an interface for its derived class. We can have pointers and references of abstract class type. If we do not override the pure virtual function in the derived class, then the derived class also becomes an abstract class.
Example: Abstract class
#include<iostream>
using namespace std;
class database {
public:
virtual void getName() = 0;
};
class Manager : public database {
public:
void getName() {
cout << “this is the manger class:\n”;
}
};
class accountant : public database {
public:
void getName() {
cout << “this is the accountant class:\n”;
}
};
class customer : public database {
public:
void getName() {
cout << “this is the customer class:\n”;
}
};
int main()
{
Manager m;
database *d;
d=&m;
d ->getName();
}
C++ Program to illustrate the abstract class and virtual functions
#include <iostream>
using namespace std;
class Base {
int x;
public:
virtual void fun() = 0;
int getX() {
return x;
}
};
class Derived : public Base
{
int y;
public:
void fun() {
cout << “fun() called”;
}
};
int main()
{
Derived d;
d.fun();
return 0;
}
Static binding
When compiler gets all the information required to call a function or all the value of variable during compile time is called static binding. It is also called early binding .It is also enhances the speed of execution of program.
Function overloading and operator overloading are the example of the static binding.
Dynamic binding
Calling a function or assigning a value to a variable at a run time is a called dynamic binding. It is also called late binding. It makes program execution flexible as it can be declared what value should be assign and which function should be called at runtime. Hence, makes the execution slower as compared to static binding.
Function overriding and virtual function are the example of dynamic binding.