Explain types of polymorphism briefly. Write down roles of polymorphism. How can we achieve dynamic polymorphism briefly? Explain with example.

This answer is restricted. Please login to view the answer of this question.

Login Now

Polymorphism means that have many form. In simple form, Polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. 

Types of Polymorphism

  • Compile time Polymorphism
  • Runtime Polymorphism
Types of Polymorphism | HAMROCSIT

1. Compile time polymorphism:

This type of polymorphism is achieved by function overloading or operator overloading.

  • Function Overloading: When there are multiple functions with same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by change in number of arguments or/and change in type of arguments.
  • Operator Overloading: C++ also provide option to overload operators. For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands , adds them and when placed between string operands, concatenates them.

2. Run Time polymorphism

This type of polymorphism is achieved by Function Overriding.

  • Function overriding on the other hand occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.

How to achieve Dymamic Polymorphism?

Dynamic Polymorphism is the polymorphism which can be achieved by runtime. It is achieve by overriding.

Overriding can be implement using in two ways.

  • Virtual functions/methods
  • Template meta programming

Here are the examples showing the implementation of dynamic polymorphism.

#include<iostream>
using namespace std;

class Base{

    public:
        virtual void baseFunction(){
            cout << "Base class perform some function . . . " << endl;
        }
};

class Child: public Base{

    public:
        void baseFunction(){
            cout << "Child class perform some new function . . . " << endl;
        }
};

int main(){

    Base *b;
    Child c;
    b = &c;
    b->baseFunction();

    return 0;

}

The the output of above code is

Child class perform some new function . . . 

Explanation:

  • If we don’t make Base class function as virtual function then Base class’s function is called instead of child class’s function.
  • If you clearly see on main function, We have set address of Child class to Base pointer. It’s means that, It must call the child function but it call base class.
  • So, We will make Base class function as virtual function.
If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .