Question 5
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
struct Base {
Base() { who(); }
virtual ~Base() { who(); }
virtual void who() const { std::cout << "B"; }
};
struct Derived : Base {
Derived() { who(); }
~Derived() override { who(); }
void who() const override { std::cout << "D"; }
};
int main() {
{ Derived d; std::cout << '|'; }
std::cout << '\n';
return 0;
}
- A BD|DB Correct
- B DD|DD
- C BB|BB
- D DB|BD
Correct Answer: Option A
While Base's constructor or destructor runs, the object behaves as a Base, so who() prints B there; in Derived's constructor and destructor it prints D.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!