Interview Questions   Tutorials   Discussions   Programs   Videos   

C++ - What is polymorphism? Explain with an example?

asked marvit August 19, 2014 03:03 AM  

What is polymorphism? Explain with an example?


1 Answers

answered By bhjahnavi   0  
Polymorphism :
Polymorphism allows routines to use variables of different types at different times. An operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways.
Poly a Greek term ability to take more than one form. Overloading is one type of Polymorphism. It allows an object to have different meanings, depending on its context. When an exiting operator or function begins to operate on new data type, or class, it is understood to be overloaded.
/*program to demonstrate function overloading */
#include <iostream>
 using namespace std;
 long add(long, long);
float add(float, float);
 int main()
 long a, b, x;
 float c, d, y;
 cout << "Enter two integers\n";
 cin >> a >> b;
 x = add(a, b);
 cout << "Sum of integers: " << x << endl;
 cout << "Enter two floating point numbers\n";
 cin >> c >> d;
 y = add(c, d);
 cout << "Sum of floats: " << y << endl;
 return 0;
 long add(long x, long y)
 long sum;
 sum = x + y;
 return sum;
 float add(float x, float y)
 float sum;
 sum = x + y;
 return sum;

   add comment

Your answer

Join with account you already have



Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!