Saturday, 28 April 2018

POLYMORPHISM IN C++



POLYMORPHISM IN C++

C++ achieves polymorphism through:
i) Function overloading
ii)Operator overloading

By the term polymorphism we understand as ‘one interface, multiple methods’. In both the ways i.e. function and operator overloading, one interface or entity is made to appear having multiple methods.

FUNCTION OVERLOADING:
In C++, two or more functions are allowed to have same name but should have different parameters declaration. When two or more functions share same name then they are said to be overloaded. This process is referred as function overloading.
For example,

#include<iostream>
using namespace std;
void sum1( int i,int j);
void sum1(float x,float y);
int main()
{ int a,b;
a=10;
b=12;
float p=10.2,q=20.6;
sum1(a,b);
sum1(p,q);
}
void sum1(int i, int j)
{ cout<<"int::"<<i+j;
}
void sum1(float x, float y)
{ cout<<"\nfloat:"<<x+y;
}

OUTPUT:
int::22
float:30.8

OPERATOR OVERLOADING:
When an operator is overloaded in C++, it takes on an additional meaning relative to a certain class retaining its old meanings.
In other words, operators can be overloaded by defining what they mean relative to a specific class.

POLYMORPHISM IN C++

POLYMORPHISM IN C++ C++ achieves polymorphism through: i) Function overloading ii)Operator overloading By the...