Polymorphism is considered to be one of the biggest advantages of using an object oriented programming. It is the property of the same object to behave differently in a different context when supplied with the same message. There are two ways in which this can be done. Compile time polymorphism which takes place at compile time and Runtime polymorphism which happens at runtime. The operator overloading and function overloading are examples of compile time polymorphism. When we encounter statements, we can see that the same message (+) is interpreted differently. Look at these definitions of three different types of data and their respective operations. The + operations which we see indicates the same message given to three different pairs of objects of different types;
When the functions are overloaded, the same function acts differently when a different set of arguments are passed to them. For example, the same operation can be executed using an overloaded function called Sum.
When the function Sum is called with int types an int addition is performed, when complex arguments are provided, real and imaginary parts are added and when an employee is added to the department, he becomes part of the department, by having two different arguments in Sum. In all of above cases, the object behaves differently, i.e. calls a different function or invokes different operator, given the same message (Sum or +), depending on the context (the arguments that we pass), thus it confirms to be a polymorphism. Both of these operations happen at compile time and thus this is called compile time polymorphism. The C++ language extends this functionality to runtime. It is possible to define, for example, classes in some hierarchy and have virtual functions defined. We can have a pointer pointing to a base class object but can call a function defined for any derived class object and decide that at run time.

Course Curriculum

Runtime polymorphism by virtual functions 00:34:00

Course Reviews


  • 5 stars0
  • 4 stars0
  • 3 stars0
  • 2 stars0
  • 1 stars0

No Reviews found for this course.

  • FREE
All Rights Reserved to Extra Period. Powered by