Polymorphism allows us to keep collections of different types of related objects. Explain how this is so using a practical example. (C++ programming)
The best way to explain the polymorphism can be by method overridding.
Example code:
#include <iostream>
using namespace std;
class polymorphismExample {
public:
static int Add(int a, int b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
} ;
int main()
{
cout<<polymorphismExample :: Add(1,2);
cout<<polymorphismExample :: Add(1,2,3);
//Add method taking many forms
return 0;
}
Output:
3 6
Here in the above example add method in the polymorphismExample class taking many forms according to the inputs.
That is the clear example of a polymorphism object taking many forms.
Get Answers For Free
Most questions answered within 1 hours.