← Quiz: C++ Interview Questions: all 10 questions
Quiz: C++ Interview Questions · Question 9 of 10
What does this print? (code)
What does this print?
#include <iostream>
using namespace std;
struct A { void show() { cout << "A"; } virtual void vshow() { cout << "a"; } virtual ~A() {} };
struct B : A { void show() { cout << "B"; } void vshow() override { cout << "b"; } };
int main() {
A *p = new B();
p->show();
p->vshow();
cout << "\n";
delete p;
return 0;
}
- A.Bb
- B.Ab
- C.Aa
- D.Ba
Answer & explanation
Correct answer: Ab
show is not virtual, so the pointer type (A) decides; vshow is virtual, so the real object (B) decides.
Can you get the rest right without peeking?
Take the timed Quiz: C++ Interview Questions quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →