← Quiz: Inheritance and Virtual Functions: all 10 questions
Quiz: Inheritance and Virtual Functions · Question 2 of 10
What does this print? (code)
What does this print?
#include <iostream>
using namespace std;
struct A { virtual void hi() { cout << "A"; } virtual ~A() = default; };
struct B : A { void hi() override { cout << "B"; } };
int main() {
A *p = new B();
p->hi();
delete p;
return 0;
}
- A.A
- B.B
- C.AB
- D.Error
Answer & explanation
Correct answer: B
hi() is virtual, so the real object's (B's) version runs.
Can you get the rest right without peeking?
Take the timed Quiz: Inheritance and Virtual Functions quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →