A False Start

Test your C++ programming skills by solving the given puzzle about object and resource management.

Puzzle code

Carefully read the code given below:

Press + to interact
#include <iostream>
#include <stdexcept>
struct Engine
{
~Engine() { std::cout << "Engine stopped\n"; }
};
struct Machine
{
Machine() { throw std::runtime_error{"Failed to start the machine"}; }
~Machine() { std::cout << "Machine stopped\n"; }
Engine engine_;
};
int main()
{
try
{
Machine machine;
}
catch (...)
{
}
}

Your task: Guess the output

Try to guess the output of the above code. Attempt the following quiz to assess your understanding.

Q

What is the expected output of the above code?

A)
Machine stopped
B)
Engine stopped
C)
Engine stopped
Machine stopped
D)
Failed to start the machine
Engine stopped
Machine stopped

Let’s discuss this code and output together in the next lesson.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.