1

I have this code:

#include <iostream>

class A {
public:
    virtual void foo() {
        std::cout << "this is A" << std::endl;
    }
};

class B : public A {
public:
    void foo() {
        std::cout << "this is B" << std::endl;
    }
};

int main() {
    A obj = B();
    obj.foo();
    std::cin.get();
    return 0;
}

The desired output is this is B, but I am getting this is A. It works when obj is A*, but is there a way to get this is B while keeping obj A?

galaxy001
  • 404
  • 5
  • 16

0 Answers0