1

There is a class Base which has a function add that inputs integers from the users and prints their sum. There's another class called Derived which publicly inherits the Base class and it also has a function add but it takes real numbers as inputs (basically float) and prints their sum.

Now what I want is for the user to input 2 numbers and the code to automatically know which function to call based on the type of input using runtime polymorphism.

For eg. if I give the inputs 2 and 3, it should print 5 and then "in base class" and if I give inputs as 3.5 and 2.5, it should print 6.0 and "in derived class".

The problem I'm facing with my code is that even if I'm giving the inputs as 2 and 3, it is calling the function of the derived class, as it is implicitly converting the integer inputs to float.

Here's my code:

#include <iostream>
using namespace std;

class Base {
public:
    virtual void add() {
        int a, b, c;
        cin >> a >> b;
        c = a + b;
        cout << c << endl;
        cout << "base" << endl;
    }
};

class Derived : public Base {
public:
    void add() {
        float a, b, c;
        cin >> a >> b;
        c = a + b;
        cout << c << endl;
        cout << "derived" << endl;
    }
};

int main() {
    Base* ptr;
    Derived d;
    ptr = &d;
    ptr->add();
    return 0;
}

I tried doing it normally and also tried using virtual but it's not helping.

Edit:

Here's the entire question: Perform addition of two number using the concept of runtime polymorphism. Here, you have to create a Base class and Derived class. The Derived class should be derived from Base class and the both class will contain an add() function. In the Base class add() function is used to add two integers and in Derived class the add() function is used to add two real numbers. Finally, the real numbers addition will be displayed as the output.

Looks like it's just a dumb question

8
  • its a derived object, you call the method of the derived class via pointer to base. That is polymorphism. Commented Jun 21, 2023 at 14:20
  • "... as it is implicitly converting the integer inputs to float" - that's not the reason. It calls add in Derived because that's the most derived type with an overridden add function. Just replace the content of the add functions with printouts. example Commented Jun 21, 2023 at 14:20
  • 1
    You seem to have a fundamental misunderstanding of how virtual functions work. By the time the user is entering values, it's already been decided whether you're in Base::add or Derived::add. Commented Jun 21, 2023 at 14:20
  • @NathanPierson Yes I know that it'll give priority to the derived function and immediately start with it. But that's what I've been asked to do, to show runtime polymorphism. What I want to confirm is that it's not possible with this specific example right? Commented Jun 21, 2023 at 14:24
  • 2
    I am afraid, we wont be able to solve the deeper issue here, that you misunderstood your task. You could try to include the exact text of the assignment in the quesiton, but actually I think it would be better to ask your tutor for clarification Commented Jun 21, 2023 at 14:35

1 Answer 1

1

Now what I want is for the user to input 2 numbers and the code to automatically know which function to call based on the type of input using runtime polymorphism.

This is not how polymorphism works. Virtual dispatch in a nutshell:

 ptr->add();

ptr is a pointer to Base. Base::add is virtual, hence the dynamic type of the object determines which method is called. The object is of type Derived, hence Derived::add is called.

This is how it is decided what method is called. Once called, it does not automatically get replaced by a different method depending on some user input.

Moreover, if you use std::cin to read integers, then the result will always be stored in that integers:

    int a, b, c;
    cin >> a >> b;

Entering 2.0 and 3.0 will not automagically turn a and b into floats. Similarly

    float a,b,c;
    cin >> a >> b;

If you enter 1 and 2 then the values stored in a and b are floating point numbers. The types of variables does not automatically change depending on user input.

Sign up to request clarification or add additional context in comments.

10 Comments

Yes I get that. What I want to confirm is that in this specific scenario, where the user is entering the numbers, it's impossible to determine which add() it will call right? Because it's happening during runtime, it'll call whichever add has the higher priority
@TanishqKohli in the first half of the answer I try to explain that the user input has no influence whatsoever on whcih method is called. In your code Derived::add is called and that method prints "derived", it cannot pritn something else.
@TanishqKohli btw there are no "priorities" here. ptr->add() can only call one method. The right one. And that is Derived::add because ptr does point to an instance of type Derived and add is virtual
@TanishqKohli it is possible to write code that calls different methods, depending on user input, but the code for this would be very different from yours, and I also do not see how polymorphism would fit in. As I commented above, the only way I see to clarify more is if you tell us what exactly is your task
Here's the entire question: Perform addition of two number using the concept of runtime polymorphism. Here, you have to create a Base class and Derived class. The Derived class should be derived from Base class and the both class will contain an add() function. In the Base class add() function is used to add two integers and in Derived class the add() function is used to add two real numbers. Finally, the real numbers addition will be displayed as the output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.