09 - C++ Polymorphism

In this section of the C++ tutorial, you will learn the following:

  • What is polymorphism
  • Polymorphism in C++ programming language
    • Function Overloading
    • Operator Overloading

9.1 C++ Polymorphism

C++ programming language allows you to implement polymorphism by overloading functions and operators.

9.2  C++ Function Overloading

In C++, function overloading allows you to create two or more functions with the same name. The compiler decides which function to call based on type, number or the sequence of parameters that are passed to these functions.

Note: In C++, the return type of function is not used to distinguish between functions with the same name.

The following statements are not valid:

void display (int i); 
long display (int i);

9.2.1 Function Overloading Example

Consider that you need to create a function that displays an integer value and function that displays a decimal value. Now, instead of creating two functions with different names, you can use function overloading. This allows you to create two functions (say display) with the same name. Depending on the type of value you pass to the display function, the compiler invokes the appropriate one.

The user of the program needs to remember only one function name (display). The user doesn’t have to worry about how the compiler figures out which display function to call. This makes the program more user-friendly.

You can overload the display function as follows:

#include <iostream.h>
using namespace std;

void display (int x)
{
    cout<<”Displaying integer value”<<x<<endl;
}    

void display (float y)
{
    cout<<”Displaying decimal value”<<y<<endl;
}

int main( )
{        
    int a=100;
    float b= 10.54;
    display(int a);
    display(float b);
}

The output of the program is as follows:

Display integer value 100

Displaying decimal value 10.54 

In the above example, we overload the display function; there are two functions with the name display. However, one displays an integer value and the other displays a decimal value. The compiler, in the above example, figures out the appropriate display function to call based on the data type being passed to it.

9.3 C++ Operator Overloading

An operator performs a specific task. For example, the ‘+’ operator adds two numbers. In C++, all operators are defined for built-in data types and you cannot create new operators. Thus, you need to overload the existing operators to work with user-defined data types.

When you overload an operator, similar to function overloading, you define the behavior of the operator in the context of user-defined data type. Let’s say you want to add two objects of a class. To do this, you need overload the ‘+’ operator. When you overload the operator, you write code that specifies how to add the two objects. 

You can look at an operator as a function; the difference is in the name. The name of the operator has a symbol that must be preceded by the operator keyword. Just as you pass parameters to a function, you pass operands to an operator.

In essence, operator overloading allows you use existing operators with user-defined data types.

9.3.1 Operator Overloading Example

The following code defines a class MyNum that has the following variable and function:

  • A public variable val
  • A constructor that initializes the  variable val
  • A member function sum that adds two objects and returns and object of the class MyNum

In main() we do the following:

  • We create two objects of the class MyNum; a and b and initialize them with the values 10 and 5 respectively
  • Next, we use the object a to call the member function sum and pass object b as a parameter
  • The result of the addition of the two objects is stored in object c of the class MyNum

The class definition and the main() function are as follows:

Class MyNum
{    
    public:
        int val;
        MyNum(int i)    //constructor
        {
            val=i;
        }
        MyNum sum(MyNum &a)    // function sum that returns an                                object of the class myNum
        {
            return MyNum(val + a.val);     //adding two objects
        }    
};

int main()
{
    MyNum a=MyNum(10);  //creating and initializing                                 object a
    MyNum b=MyNum(5);  //creating and initializing                                  object b

   MyNum c= a.sum(b);
}

A more intuitive and easier way to add two objects is as follows:    

     MyNum c = a+b;

This can be done by overloading the ‘+’ operator as follows:

MyNum operator+(MyNum &a)
        {
            return MyNum(val + a.val);
        } 

Now in main (), you can write the following statement:

     MyNum c = a+b; 

The name of the function sum is replaced by the word operator+. The operator ’+’ is an alternate syntax for calling the function sum.

The class MyNum with an overloaded operator + and the main() function are as follows:

Class MyNum
{    
    public:
        int val;
        MyNum(int i)    //constructor with parameter
        {
            val=i;
        }
        MyNum operator+(MyNum &a)    // Overloading the +                                         operator
        {
            return MyNum(val + a.val);     //adding two                                             objects of the class Mynum
        }    
};

int main()
{
    MyNum a= MyNum(10);  //creating and initializing                                 object a
    MyNum b= MyNum(5);   //creating and initializing     

    MyNum c= a+b;
}

Like us on Facebook