Monsters on the Move
Test your C++ programming skills by solving the given puzzle about constructor behavior and value categories.
We'll cover the following
Puzzle code
Carefully read the code given below:
#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.
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.