C++ Quiz7 of 173
Difficulty:
⬅️ Prev: 6Quiz ID: 7Next: 8 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

class A {
public:
  void f() { std::cout << "A"; }
};

class B : public A {
public:
  void f() { std::cout << "B"; }
};

void g(A &a) { a.f(); }

int main() {
  B b;
  g(b);
}
Hint

virtual...

Reveal answer

A

Explanation

As long as A::f() is not virtual, A::f() will always be called, even if the reference or pointer is actually referring to an object of type B.

⬅️ Prev: 6Quiz ID: 7Next: 8 ➡️