+ 4

What is polymorphism?

In simple words

31st Dec 2016, 6:42 AM
Bhargav Makkena
4 Answers
+ 6
polymorphism: in general it means more than one form. C++ provides polymorphism, a function having same can perform diffrent operations depending on parameters passed to it. this is called function overloading. eg. consider a function area. it can be used as: int area(int l, int b) //area of rectangle { return l*b; } int area(int side) //area of square { return side*side; } now compiler will decide which function to call according to parameters. area(2,4) area(6)
31st Dec 2016, 6:50 AM
Ravi Kumar
Ravi Kumar - avatar
+ 7
great answer Mr.Kumar
31st Dec 2016, 6:51 AM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 4
Usually, when we speak about polymorphism in c++, we don't mean function overloading or generic programming, we mean polymorphism via subtyping. Therefore it is the possibility of a method of an object to have different behaviour depending on the "real" type of the object. For example: class Animal { public: virtual void speak() { cout << "hello"; } }; class Cat : public Animal { public: void speak() { cout << "meow"; } }; int main() { Cat cat; Animal* a = &cat; a->speak(); } output: meow
31st Dec 2016, 9:49 AM
Simon Lepasteur
Simon Lepasteur - avatar
+ 3
polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.
31st Dec 2016, 12:48 PM
Priyanshi
Priyanshi - avatar