Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we actually need runtime polymorphism?

I'm trying to understand a polymorphism but I don't understand why we need runtime polymorphism if static polymorphism works fine for calling members of a class.

Like, suppose this was a problem.

#include <bits/stdc++.h>
using namespace std;
class base{
    public:
        virtual void fun(){
            cout<<"base called"<<endl;
        }
};
class derived:public base{
    public:
        void fun(){
            cout<<"derived called"<<endl;
        }
};
int main() {

    base b,*b1;
    derived d;
    b1 = &d;
    b1->fun();
    // b.fun();
    // d.fun();
}

suppose this was my code and I want to access the function of derived or base class and I was able to do it by simply creating an object of that class so if there is no problem then why we try to call the object using references (runtime polymorphism). Can someone explain the actual need for runtime polymorphism or if it is possible can you explain it by using any real-life scenarios??

like image 218
Shivam Jain Avatar asked Jul 25 '26 06:07

Shivam Jain


2 Answers

Polymorphism is considered as one of the important features of Object-Oriented Programming. In C++ polymorphism is mainly divided into two types:

  • Compile-time Polymorphism: This type of polymorphism is achieved by function overloading or operator overloading.

  • Runtime Polymorphism: This type of polymorphism is achieved by Function Overriding.

Now consider the following scenario.

Suppose we have a base class named Shape which has the following interface.

class Shape {
public:
    Shape(int init_x, int init_y);
    virtual ~Shape() = default;

    virtual void scale(int s) = 0;
protected:
    int x;
    int y;
};

Now we want to inherit two other classes named Rectangle and Circle from it.

class Rectangle : public Shape {
public:
    Rectangle(int init_x, int init_y, int w, int h);
    void scale(int s) override;
private:
    int width;
    int height;
};
class Circle : public Shape {
public:
    Circle(int init_x, int init_y, int r);
    void scale(int s) override;
private:
    int radius;
};

As you may know, circle and rectangle shapes, have different implementation for their scale method, so I can't implement that in the Shape class.

Now suppose we have a program that stores all the shapes in a container like vector<Shape*> (e.g. it gets all the shapes from the user once and store them in this container).

If we want to use scale method on one of the shapes, we don't know actually which kind of shape we are dealing with, but it will bind our scale method call to its appropriate implementation.

like image 73
Mohammad Moridi Avatar answered Jul 26 '26 20:07

Mohammad Moridi


Polymorphism is useful any time that the software can't be told at compile time exactly what everything is going to be at runtime, or when you need a container to be able to hold a heterogeneous assortment of things that all implement a common interface.

A good example is UI toolkits. Pretty much all UI toolkits have a concept of a container that can hold other widgets, but the UI toolkit author has no idea what widgets you are going to put into the container. Since the container needs to be able to deal with any kind of widget, polymorphism is the easiest way to achieve this.

like image 33
cdhowie Avatar answered Jul 26 '26 19:07

cdhowie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!