Question 17
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
struct Pager {
virtual int page_size(int requested = 10) const = 0;
virtual ~Pager() = default;
};
struct WebPager : Pager {
int page_size(int requested = 25) const override { return requested > 50 ? 50 : requested; }
};
int main() {
WebPager w;
const Pager& p = w;
std::cout << p.page_size() << ' ' << w.page_size() << ' ' << p.page_size(80) << '\n';
return 0;
}
- A 10 25 50 Correct
- B 25 25 50
- C 10 10 80
- D 25 10 50
Correct Answer: Option A
Default arguments come from the static type of the call: the interface's 10 through p, WebPager's 25 through w. Keep defaults identical (or avoid them) in interfaces.
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!