LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Predict the Output · Question 8

Designated initialisers with default member values Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

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

main.cpp
#include <iostream>
#include <string>

struct Options {
    std::string format = "pdf";
    int copies = 1;
    bool color = false;
    double scale = 1.0;
};

int main() {
    Options a{};
    Options b{.copies = 3, .scale = 0.5};
    Options c{.format = "png", .color = true};
    std::cout << a.format << a.copies << a.color << ' ' << b.format << b.copies << b.color << b.scale << ' ' << c.format << c.copies << c.color << '\n';
    return 0;
}
  • A pdf10 pdf300.5 png11 Correct
  • B pdf10 300.5 png11
  • C pdf10 pdf30 png11
  • D pdf11 pdf300.5 png01
Correct Answer: Option A

Members that are not named keep their default member initialisers.

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