Polymorphism in C++ - BCA, B.Tech_info

Easy way to learn

Saturday, August 18, 2018

Polymorphism in C++

Definition:
      Polymorphism is another OOP concept. Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors at different instances. The instances depend upon the type of data used in the operation.

The word polymorphism means having many forms. In simple word, we can define polymorphism as the ability of a message to be displayed in more than one form. Real life example of polymorphism, a person at a same time can have same characteristic. 

Polymorphism is mainly divided into two types:
  • Compile time polymorphism 
  • Runtime polymorphism 

Compile time polymorphism

        This type of polymorphism is achieved by function overloading or operator overloading.
Types of Compile time polymorphism -
  1. Function overloading
  2. Operator overloading

1) Function overloading

           When there are multiple function with same name but different parameters then those functions are said to be overloaded. 
Overloading in C++ refers to the use of the same thing for different purposes. This means that we can use the same function name to create functions that perform a variety of different tasks. This is also known as polymorphism. Using the concepts of function overloading, we can design a family of functions with one function name but with different argument list. 

// Declarations                                              

int div(int x, int y);                                                  //prototype 1
int mul(int x, int y, int z);                                        //prototype 2
float sub(float a, float b);                                        //prototype 3

// Function calls

cout << div(3,4);                                                     //uses prototype 1
cout << mul(2, 3, 4);                                               //uses prototype 2
cout << sub(10.2, 12.5);                                          //uses prototype 3

The function would perform different operation depending upon the argument list in the function call. A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.



Example program



#include<iostream.h>
using namespace std;

int vol(int);
float vol(float, int);

int main()
{
   cout << vol(5) <<"\n";
   cout << vol(2.5, 8) <<"\n";
   return 0;
}
int vol(int s)
{
   return(s*s*s);
}
float vol(float r, int h);
{
   return(3.14*r*r*h);
}

OUTPUT:

125

157.26



2) Operator overloading:

The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.  C++ provide option to overload operators.
For example, we can make the operator ('+') for string to concatenate two strings. We know that, this is the additional operator whose task is to add two operands. So a single operator '+' when placed between two operands, adds them and when placed between strings operand, concatenate them. Basically, operator overloading is used because it makes program more readable as the operator which are used for basic data types can also be used for user-defined data types


C++ program to overload unary minus (-) operator:


#include <iostream>
#include <conio.h>
using namespace std;

class example
{
    int a,b;
    public:
        void input()
        {
            cout<<"Enter a and b: ";
            cin>>a>>b;
        }
        void operator -()   //operator function as a member function
        {
            a=-a;
            b=-b;
        }
        void display()
        {
            cout<<"a="<<a<<endl<<"b="<<b<<endl;
        }
};

int main()
{
    example e;
    e.input();
    cout<<"Before overloading unary minus operator is "<<endl;
    e.display();
    -e;
    cout<<"After overloading unary minus operator is"<<endl;
    e.display();
    getch();
    return 0;
}

OUTPUT:

Enter a and b: 4 -5

Before overloading unary minus operator is
a=4
b=-5
After overloading unary minus operator is
a=-4

b=5


Runtime polymorphism:



This type of polymorphism is achieved by function overriding.

Function overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.


No comments:

Post a Comment

Polymorphism in C++