The word polymorphism means having many forms. Typically, 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.
Types of polymorphism :
1. Static Polymorphism or Compile Time
2.
Static Polymorphism : Static polymorphism refers to an entity existing in different physical forms simultaneously. Static polymorphism involves binding of functions based on the number, type, and sequence of arguments. The various types of parameters are specified in the function declaration, and therefore the function can be bound to calls at compile time. This form of association is called early binding. The term early binding stems from the fact that when the program is executed, the calls are already bound to the appropriate functions.
It is divided into two types :
1. Function overloading
2. Operator overloading
1. Function overloading : Function overloading means two or more functions can have the same name but either the number of arguments or the data type of arguments has to be different. Return type has no role because function will return a value when it is called and at compile time compiler will not be able to determine which function to call.
Function overloading is also known as compile time polymorphism.
For example :
int Add(int X, int Y) { return(X+Y); } double Add(double m, double n) { return(m+n); }
2. Operator overloading : One of the nice features of C++ is that you can give special meanings to operators, when they are used with user-defined classes. This is called operator overloading. You can implement C++ operator overloads by providing special member-functions on your classes that follow a particular naming convention. For example, to overload the + operator for your class, you would provide a member-function named operator+ on your class.
The following set of operators is commonly overloaded for user-defined classes:
- = (assignment operator)
- + - * (binary arithmetic operators)
- += -= *= (compound assignment operators)
- == != (comparison operators)
For example :
class temp { public: int count; void operator ++() { count=count+1; } };
Rules for operator overloading
Every programmer knows the concept of operation overloading in C++. Although it looks simple to redefine the operators in operator overloading, there are
certain restrictions and limitation in overloading the operators. Some of them are listed below:
- Only existing operators can be overloaded. New operators cannot be overloaded.
- The overloaded operator must have at least one operand that is of user defined type.
- We cannot change the basic meaning of an operator. That is to say, We cannot redefine the plus(+) operator to subtract one value from the other.
- Overloaded operators follow the syntax rules of the original operators. They cannot be overridden.
- There are some operators that cannot be overloaded like size of operator(sizeof), membership operator(.), pointer to member operator(.*), scope
resolution operator(::), conditional operators(?:) etc.
- We cannot use "friend" functions to overload certain operators . However, member function can be used to overload them. Friend Functions can- not be
used with assignment operator(=), function call operator(()), subscripting operator([]), class member access operator(->) etc.
- Unary operators, overloaded by means of a member function, take no explicit arguments and return no explicit values, but, those overloaded by means of a
friend function, take one reference argument .
- Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two
explicit arguments.
- When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class.
- Binary arithmetic operators such as +,-,* and / must explicitly return a value. They must not attempt to change their own arguments.
Dynamic polymorphism : Dynamic Polymorphism refers to an entity changing its form depending on the circumstances. A function is said to exhibit dynamic polymorphism when it exists in more than one form, and calls to its various forms are resolved dynamically when the program is executed. The term late binding refers to the resolution of the functions at run-time instead of compile time. This feature increases the flexibility of the program by allowing the appropriate method to be invoked, depending on the context.
Virtual function is also known as dynamic polymorphism.
What is virtual function ?
A virtual function is a member function that is declared within a base class and redefined by a derived class. To create virtual function, precede the function's declaration in the base class with the keyword virtual. When a class containing virtual function is inherited, the derived class redefines the virtual function to suit its own needs.
Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.
For defining virtual function the keyword virtual is used with function name.
For example :
class abc { public : int w,h; void values( int a, int b) { w=a;h=b; } virtual int area () { return 0; } };