Dynamic Polymorphism (Runtime Polymorphism) – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#4650    siteicon   siteicon  

Dynamic Polymorphism (Runtime Polymorphism)

Demonstration of a kind of polymorphism action when the program is rum.

Learning Objectives

  • Understanding how virtual function can allow polymorphism at runtime.

Source Code

TC++ #4650


Source Code

Run Output

Mammals don't fly
Bats are exceptions, they fly

Code Understanding

class Mammal {
public: virtual void fly(){cout<<“Mammals don’t fly”<<endl;} };
The fly() function in parent class Mammal is virtual as it is expected to be overridden in its child class.

class Bat:public Mammal { public: void fly(){cout<<“Bats are exceptions, they fly”<<endl;}};
The fly() function in this derived class is an overridden function and is called when object from Bat is in use

void Doesfly(Mammal *m) { m->fly();}
Since the object of Parent Mammal and Child Bat are type compatible so the passed parameter can be reference of object of Parent or of Child. When Parent object is passed function from parent is called, if object of child is passed function from child is called. This is basically a function that is following the dynamic or runtime polymorphism model.

int main() {
Bat b; Mammal m;
Objects of child class Bat and parent class Mammal are declared.


This function is called with reference address of the parent object.

This function is called with reference address of the child object.

Note: This is a decent implementation of polymorphism in C++ as here function is same, parameter type is same but on the basis of from where the parameter originates at runtime the code portion to be called is changed.

return 0;}


  • Dynamic polymorphism is also called runtime polymorphism as the decision of which path a action will take is decided at the runtime. This means that based on input condition it reaches a certain reference point which is not fixed at the time of compilation.
  • Some people also classify this type of polymorphism as of class subtype based polymorphism. A good article with more examples is here
  • You can also read the polymorphism concept article here.

Common Errors

  • Care needs to be taken in referencing the pointer variables while passing as parameter in the function definition part and as addressof of an object type while calling the function.

Suggested Filename(s): dyn-poly.cpp,dynamic-polymorphism.cpp


sunmitra| Created: 7-Sep-2018 | Updated: 7-Sep-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues