LearnFrenzy
Sign in Take a free test

Constructors and Destructors · Predict the Output · Question 12

Constructors and destructors for an array of objects Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 12 Back to Exercise

What is the output of the following C++ program?

main.cpp
#include <iostream>

struct Cell {
    static inline int made = 0;
    int id;
    Cell() : id(++made) { std::cout << id; }
    ~Cell() { std::cout << '~' << id; }
};

int main() {
    {
        Cell grid[3];
        std::cout << '|';
    }
    std::cout << '\n';
    return 0;
}
  • A 123|~3~2~1 Correct
  • B 123|~1~2~3
  • C |123~3~2~1
  • D 111|~1~1~1
Correct Answer: Option A

Array elements are constructed from first to last and destroyed in reverse order.

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