Polymorphism (a Greek word meaning having multiple forms) is the ability of an entity such as a function or a message to be processed in more than one form. It can also be defined as the property of an object belonging to a same or different class to respond to the same message or function in a different way. For example, if a message change_gear is passed to all the vehicles then the automobiles will respond to the message appropriately however, the pulled vehicles will not respond. The concept of polymorphism plays an important role in OOP as it allows an entity to be represented in various forms.
In C++, polymorphism can be achieved either at compile-time or at run-time. At compile-time, polymorphism is implemented using operator overloading and function overloading. However, at run-time, it is implemented using virtual functions
Operator overloading is the process that enables an operator to exhibit different behavior, depending on the data provided. For example, when the ‘+’ operator is used with two numbers, it adds the two numbers and produces the sum. However, if it is used with two strings, it concatenates the two strings and produces the third concatenated string
Similarly, a single function can behave differently depending on the type of data provided. For example, in Figure, the function Add can be used to add two integers and two float-point numbers. This form of polymorphism is known as function overloading.
Consider another example in which three different classes, square, rectangle and circle are derived from the base class geometrical_shapes. The function area () of the base class is implemented in different ways in all its derived classes and a call to a particular function is determined at run-time. This form of polymorphism is called run-time polymorphism.
Message passing is a process of interacting between different objects in a program. A program following the object-oriented paradigm comprises a set of objects each with a set of data and functions. When the program is executed, these objects interact or communicate with each other by sending and receiving messages. The messages are exchanged by calling the member functions of the classes. Any object of a class that wants to communicate with the object of another class requests the object to invoke the required member function of its class. This function call is different from the normal function call as in this case the sending object is sending a request for the execution of the function. However, the receiving object may or may not accept the request depending on whether the function forms the public interface or it is hidden from the outside world. Thus, this form of communication is called message sending and not an ordinary function call. For example, consider two classes Product and Order. The object of the Product class can communicate with the object of the Order class by sending a request for placing order
Dynamic binding is the process of linking of a function call to the actual code of the function at run-time. That is, in dynamic binding, the actual code to be executed is not known to the compiler until run-time.
The concept of dynamic binding is implemented with the help of inheritance and run-time polymorphism (virtual functions). For example, consider the class hierarchy shown in Figure. Each derived class has the same function area () however, with different body. At run-time, depending on the object being referenced, the desired function will be called.