Pergunta de entrevista da empresa BNY

what is polymorphism?

Resposta da entrevista

Sigiloso

22 de jul. de 2019

The word polymorphism means having many forms. In simple words, 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 the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism. Polymorphism is considered as one of the important features of Object Oriented Programming. In C++ 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. Function Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments. Rules of Function Overloading filter_none edit play_arrow brightness_4 // C++ program for function overloading #include using namespace std; class Geeks { public: // function with 1 int parameter void func(int x) { cout using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i =0) {real = r; imag = i;} // This is automatically called when '+' is used with // between two Complex objects Complex operator + (Complex const &obj) { Complex res; res.real = real + obj.real; res.imag = imag + obj.imag; return res; } void print() { cout using namespace std; class base { public: virtual void print () { coutprint(); // Non-virtual function, binded at compile time bptr->show(); return 0; } Output: print derived class show base class To learn runtime polymorphism in details visit this link.