Monsters on the Move

Test your C++ programming skills by solving the given puzzle about constructor behavior and value categories.

Puzzle code

Carefully read the code given below:

Press + to interact
#include <iostream>
struct Monster
{
Monster() = default;
Monster(const Monster &other)
{
std::cout << "Monster copied\n";
}
Monster(Monster &&other)
{
std::cout << "Monster moved\n";
}
};
struct Jormungandr : public Monster
{
Jormungandr() = default;
Jormungandr(const Jormungandr &other) : Monster(other)
{
std::cout << "Jormungandr copied\n";
}
Jormungandr(Jormungandr &&other) : Monster(other)
{
std::cout << "Jormungandr moved\n";
}
};
int main()
{
Jormungandr jormungandr1;
Jormungandr jormungandr2{std::move(jormungandr1)};
}

Your task: Guess the output

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

The possible console output lines are given below on the right side column. Place them in the correct sequence.

Drag and drop the cards in the blank spaces.


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.