ProgrammingVery hardProduct company OA
What is printed?
What is printed?
struct A { virtual void f(int x) { std::cout << "A::f(int) " << x; } };
struct B : A { virtual void f(double x) { std::cout << "B::f(double) " << x; } };
int main() {
B b;
A *p = &b;
p->f(5);
}- A. B::f(double) 5
- B. A::f(int) 5
- C. Compilation error: ambiguous call
- D. A::f(int) 5B::f(double) 5
Correct answer: B. A::f(int) 5
B::f(double) is a DIFFERENT signature from A::f(int), so it does not override A::f(int) — it's a separate virtual function. Since p's static type is A*, only A's member list (which has f(int)) is visible for overload resolution, and it IS virtual, so it dispatches based on A's vtable entry for f(int), which b inherits unchanged. Prints A::f(int) 5.
More OOP with C++ and Java questions
- What is printed? cpp struct Base { virtual void f() const { std::cout << "Base::f const"; } }; struct Derived : Base { void f() {…
- What is printed? cpp struct IWalk { virtual void move() = 0; }; struct ISwim { virtual void move() = 0; }; struct Duck : IWalk, ISwim {…
- What is printed? cpp struct Base { static void hello() { std::cout << "Base::hello"; } }; struct Derived : Base { static void hello() {…
- What is printed? cpp struct Base { virtual Base clone() { std::cout << "Base::clone "; return new Base(this); } }; struct Derived : Base {…
- What is printed? cpp class Base { public: Base() { std::cout << "Base() "; } virtual void show() { std::cout << "Base::show"; } }; class…
- What is printed? cpp class Complex { double re, im; public: Complex(double r, double i) : re(r), im(i) {} Complex operator+(const Complex&…