Why we actually need runtime polymorphism

0 votes

I'm attempting to grasp polymorphism, but I'm not sure why we require runtime polymorphism because static polymorphism works just well for calling class members.

Assume there was an issue.

#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();
}

If this were my code, and I wanted to access the function of a derived or base class, I could do so by simply creating an object of that class, why would we try to call the object via references? (runtime polymorphism). 

Can someone explain the actual requirement for runtime polymorphism, or, if feasible, explain it using any real-life scenarios?

Jun 9 in C++ by Nicholas
• 2,520 points
10 views

1 answer to this question.

0 votes

One of the most significant elements of Object-Oriented Programming is polymorphism. Polymorphism in C++ is primarily classified into two types:

Compile-time Function overloading or operator overloading is used to accomplish this form of polymorphism.

Runtime Polymorphism: Function Overriding is used to accomplish this form of polymorphism.

Take a look at the following scenario.

Let's say we have a Shape base class with 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;
};

We now wish to inherit two more classes from it: Rectangle and Circle.

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, the scale function for circle and rectangle forms is implemented differently, hence I can't implement it in the Shape class.

Assume we have a software that saves all the shapes in a vectorShape*> container. (For example, it obtains all of the user's forms at once and stores them in this container.)

We don't know which sort of shape we're working with when we use the scale method on one of the shapes, but it will link our scale method call to the proper implementation.

answered Jun 10 by Damon
• 4,240 points

Related Questions In C++

0 votes
1 answer

What is compile-time polymorphism and why does it only apply to functions?

"Compile time polymorphism" used to signify function overloading.  It only applies to functions because that's all you can overload. Templates in modern C++ modify this.  One example has previously been provided by Neil Butterworth.  Another technique is template specialisation.  As an example: #include <iostream> #include <string> template <class T> struct my_template { ...READ MORE

answered Jun 20 in C++ by Damon
• 4,240 points
9 views
0 votes
1 answer

why are copy constructors needed and what are the cases where they are very helpful?

A copy constructor is a member function ...READ MORE

answered May 31 in C++ by Damon
• 4,240 points
36 views
0 votes
1 answer

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
39 views
0 votes
1 answer

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
38 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7 in C++ by Damon
• 4,240 points
15 views
0 votes
0 answers

Is hiding implementation detail Encapsulation or Abstraction?

I have seen some people defining abstraction ...READ MORE

May 6 in Java by narikkadan
• 10,840 points
58 views
0 votes
1 answer

Why do we need virtual functions in C++?

A virtual function is a base class member function that we expect to redefine in derived classes. In the base class, a virtual function is used to ensure that the function is overridden.  This is especially true when a pointer from a base class points to an object from a derived class. For example, consider the code below: class Base ...READ MORE

answered May 27 in Others by Damon
• 4,240 points
12 views
0 votes
1 answer

When to use virtual destructors?

When you want to delete an instance of a derived class using a pointer to the base class, virtual destructors come in handy: class Base { // ...READ MORE

answered Jun 7 in C++ by Damon
• 4,240 points
23 views
0 votes
1 answer

Why does C++ need the scope resolution operator?

No. There is no scope resolution operator ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
16 views
0 votes
1 answer

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

answered Jun 21 in C++ by Damon
• 4,240 points
17 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP