Polymorphism In C++


Definition

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 to 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. 

Types of Polymorphism:

C++ provides three different types of polymorphism.

  • Virtual functions
  • Function name overloading
  • Operator overloading

In addition to the above three types of polymorphism, there exist other kinds of polymorphism:

  • run-time
  • compile-time
  • ad-hoc polymorphism
  • parametric polymorphism

Other types of polymorphism defined:

  • run-time: The run-time polymorphism is implemented with inheritance and virtual functions.
  • compile-time: The compile-time polymorphism is implemented with templates.
  • ad-hoc polymorphism: If the range of actual types that can be used is finite and the combinations must be individually specified prior to use, this is called ad-hoc polymorphism.
  • parametric polymorphism: If all code is written without mention of any specific type and thus can be used transparently with any number of new types it is called parametric polymorphism.

Polymorphism Explanation:

A   polymorphic function or operator has many forms. For example, in c++ the division operator is polymorphic. If the arguments to the division operator are integral, then integer division is used. However, if one or both arguments are floating –point then floating –point division is used.object oriented programming language support polymorphism, which is characterized by the phrases,    "ONE    INTERFACE,    MULTIPLE       METHODS".A real world example of polymorphism is a thermostat. no matter what type of furnace your house has (gas, oil, electric etc.)


In your program, you will create three specific version of these function one for each type of stack, but names of the function will be the same. 
In C++, a function name or operator can be overloaded.A function is called based on its signature which is the list of argument types in its parameter list.

Polymorphism Explanation With Example

For example, in the division expression
a/b  the result depends on the arguments being automatically coerced to the widest type. so if both arguments are integer, the result is an integer division.But if one or both arguments are floating-point, the result is floating-point.
Another example is the output statement             

cout<<a;

Where the shift operator << is invoking a function that is able to output an object of the type.  A technique for implementing a package of routines to provide a shape type could rely on  a comprehensive structural  description of any shape

for instance,

Struct shape
{
    enum {
        circle, rectangle, ?.
    } e - val;
    double center, radius;

    // other code
};

would have all the members necessary for any shape currently drawable in our system.. It would also have an enumerator value so that it could be identified.The area routine would then be written as:

double area(shape *s) {
    switch (s->e - val) {
        case circle:return (PI * s > radius * s > radius);
        case rectangle:return (s > height * s > width);
     // other code
    }
};

 An additional case in the code body and additional members in the structure are needed.Unfortunately, these would have ripple effects throughout our entire code body.

C++ code following this design uses shape as an abstract class containing one or more pure virtual functions, as shown in the following code.

//shape is an abstract base class

class shape {
public:
    virtual double area() = 0;
};

class rectangle : public shape
{    
public:
    rectangle double area()
    {
        return (height * width)           
    }
private:
    double height, width;    
};

class circle : public shape
{
public:
    double area() 
    {
        return (3.14159 * radius * radius);   
    }
private:
    double radius;
};

the client code for computing an arbitrary area is polymorphic the appropriate area() function is selected at run time. 

shape * ptr _shape;
cout << "area =" << ptr_shape->area();

Now imagine is improving our hierarchy of types by

developing a square class:

Class square : public rectangle
{
public:
    square(double h) : rectangle(h, h)
    {
        
    }
    double area()
    {
        return (rectangle::area());   
    }
};

The client code remains unchanged. This was not the case with the non-oops code.

Hierarchical design should minimize interface parameter passing. Each layer tends to absorb, within its implementation, state detail that is affected by function invocation. This practice is almost universally condemned because it leads to opaque side-effect-style coding that is difficult to debug, revise and maintain. It is the compiler's job to select the specific action as it applies to each situation.

The first object -oriented programming language were interpreters, polymorphism was, of course, supported at run-time. However,c++ is a compiled language.Therefore, in c++, both run-time and compile-time polymorphism are supported.