Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring too many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. Polymorphism refers to codes, operations or objects that behave differently in different contexts.
Types of Polymorphism:
C++ provides two different types of polymorphism.

  • run-time
  • compile-time

run-time:
The appropriate member function could be selected while the programming is running. This is known as run-time polymorphism. The run-time polymorphism is implemented with inheritance and virtual functions.

  • Virtual functions

Virtual functions
            A function qualified by the virtual keyword. When a virtual function is called via a pointer, the class of the object pointed to determines which function definition will be used. Virtual functions implement polymorphism, whereby objects belonging to different classes can respond to the same message in different ways.
compile-time:
The compiler is able to select the appropriate function for a particular call at compile-time itself. This is known as compile-time polymorphism. The compile-time polymorphism is implemented with templates.

  • Function name overloading
  • Operator overloading

1. Operator Overloading

                        The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.

 The general form of an operator function is:
                       
                                    return type classname: : operator(op-arglist)
                                    {
                                                Function body
                                    }

The process of overloading involves the following steps:
                       
1. Create a class that defines the data type that is to be used in the overloading operation.
2. Declare the operator function operator op () in the public part of the class. It may be either a member function or a friend function.
            3. Define the operator function to implement the required operations.

2. Function Overloading
                       
                        Using a single function name to perform different types of tasks is known as function overloading.

                        Using the concept of function overloading, design a family of functions with one function name but with different argument lists. The function would perform different operations depending on the argument list in the function call. The correct function to be invoked is determined by checking the number and type of the arguments but not on the function type.