LearnFrenzy
Sign in Take a free test

Abstract Classes and Interfaces · Predict the Output · Question 17

Default arguments declared on an interface Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 17 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links