C++ Polymorphism

Polymorphism is concept of object oriented programming (OOPs). they are combination of two greek words poly (many) and morphism (many forms). polymorphism are similarly to take more than one form.

Example of Polymorphism

Suppose you are job in multinational software company. When you come to office then you are a employee in this time. when you are come to markets and purchase any instruments and foods. then this time you are a consumers. When you come to hospital for treatment then you are patients in this time. so this process is similar like a Polymorphism. one people are perform multiple tasks of special situations.

In programming are similar when same function and operator are perform many task and operations. then this process is called polymorphism.

Type of Polymorphism

Polymorphism are two types.

Compile time polymorphism

Compile time polymorphism are called static polymorphism and early binding. in this process compiler are understand the behaviours of function and operator which are provide new functionality in our source code. There are same signature of function and operator and they are perform different role in different scopes. there is two types, function overloading and operator overloading.

Function overloading : function overloading is process there are same name of function but different is number of arguments,sequence of arguments and data types. see the examples and understand this points.

 //function overloading example
  //Number of arguments are different
  void binary(int,int); //function overloading 
  void binary(float,int,double); //function overloading 

In this situation declaring of a two binary function in same source code and same class. observe that there are accept different number of arguments and parameters. so this are valid for function overloading.

Another example.

 //function overloading example
  //Different data type of arguments
  void binary(int,int); //function overloading 
  void binary(float,int); //function overloading 

In this case number of arguments are same but there accepted parameter data type are different. so this is valid example of function overloading. note that in this case first argument of both function parameter are different.

Another example

 //function overloading example
  //sequence of parameter
  void binary(int,float); //function overloading 
  void binary(float,int); //function overloading 

If same name and sequence of parameter are different then this is valid situations of function overloading.

Program for function overloading

/*
  Example of function overloading
*/
#include<iostream>
using namespace std;
//class Book
class Book{
  public:
  /*member function of class*/
  float price(int,float);
  void price(int,int,float);
  float price(float,int);
};
float Book::price(int quantity,float price){
  cout<<"Function 1 is execute"<<endl;
    //logic here
  return 1;
}

void Book::price(int quantity,int discount,float price){
  cout<<"Function 2 is execute"<<endl;
  //logic here
}
float Book::price(float mrp, int quantity){
  cout<<"Function 3 is execute"<<endl;
  //logic here
    return 0;
}
int main(){
    //create book object b1
  Book b1;
  //call member function
  b1.price(3,10.3f);
  b1.price(60.50f,2);
  b1.price(4,50,50.90f);
}
Output
Function 1 is execute
Function 3 is execute
Function 2 is execute

Operator overloading : C++ are allowing to overload function and operators. help of this feature we can perform complex operator very efficiently.

Example to overload ++ perfix increment operator.

/*
Example  Prefix ++ Increment Operator Overloading 
*/
#include<iostream>
using namespace std;

class Bonus{

  int amount;
  public:
    Bonus(int);
    //overload operator function
    void operator++();
    void blance();
};
Bonus::Bonus(int inital){
  amount=inital;
}
void Bonus::operator++(){
  ++amount;
}
void Bonus::blance(){
  cout<<" Blance :"<<amount <<endl;
}
int main(){
  //create obj and assign initial value
   Bonus obj(1000);

   obj.blance();
   int count=0;
   while(count<5){
    //increment operator overload
     ++obj;
     obj.blance();
     count=count+1;
   }
}
Output
 Blance :1000
 Blance :1001
 Blance :1002
 Blance :1003
 Blance :1004
 Blance :1005

similar we are overload various operator but not all. see more information about operator overloading

Runtime polymorphism

runtime polymorphism are occurred in when we inherit base class properties into derived class. and there are same method (function) are exist with same signature of both class. We are solved this problem by virtual function. In this process create base class pointer. and assign derived class object reference.

/*
  Example Of dynamic binding
  using virtual function.
*/
#include<iostream>
using namespace std;
class Food{

  public:
  //virtual function  
  virtual void item();
  virtual void dishes();
  void menu();
};
class Recipe :public Food{
  public:
    void item();
    void dishes();
};

void Food::item(){
  cout<<"This is Food items"<<endl;
}
void Food::dishes(){
  cout<<"Sweet Food dishes"<<endl;
}
void Food::menu(){
  cout<<"Today Special  -> Italy Sambhar"<<endl;
}

void Recipe::item(){
  cout<<"This is Recipe items"<<endl;
}
void Recipe::dishes(){
  cout<<"Spice Recipe dishes"<<endl;
}
int main(){
    //create pointer variable of Food class
  Food *food;
  //create object of Recipe
  Recipe sweet;
  food =&sweet;
  //Early binding
  food->menu();
  food->item(); //dynamic binding
  food->dishes(); //dynamic binding

  Food dream;
  food=&dream;
  food->item();// dynamic binding
}
Output
Today Special  -> Italy Sambhar
This is Recipe items
Spice Recipe dishes
This is Food items

more information about virtual function post.

Advantages of Polymorphism

Help of polymorphism We can overload function and operator. That are very useful feature. normally we can overloaded constructor of class. And assigning initial value to data member of class. This are very effective and efficient. see the example.

/*
Example  Prefix ++ Increment Operator Overloading 
*/
#include<iostream>
using namespace std;

class Shaps{
  int x,y,z;
  public:
    Shaps();
    Shaps(int,int);
    Shaps(int,int,int);
    void display();
};
Shaps::Shaps(){
  //default
  x=y=z=10;
}
Shaps::Shaps(int v1,int v2){
  x=v1;
  y=v2;
  z=y+x;
}
Shaps::Shaps(int v1,int v2,int v3){
  x=v1;
  y=v2;
  z=v3;
}

void Shaps::display(){
  cout<<"X :"<<x<<endl;
  cout<<"Y :"<<y<<endl;
  cout<<"Z :"<<z<<endl;
}
int main(){
  //Shaps class object
  Shaps s1;
  Shaps s2(10,20);
  Shaps s3(2,4,7);

  //display results
  cout<<"Object s1 info"<<endl;
  s1.display();
  cout<<"Object s2 info"<<endl;
  s2.display();
  cout<<"Object s3 info"<<endl;
  s3.display();
}
Output
Object s1 info
X :10
Y :10
Z :10
Object s2 info
X :10
Y :20
Z :30
Object s3 info
X :2
Y :4
Z :7

© 2019, regularcodes.com, All rights reserved