Search⌘ K
AI Features

How Many Degrees?

Explore the behavior of C++ code through a puzzle that asks you to predict its output. This lesson helps you deepen your understanding of constructors, destructors, and code nuances by analyzing the code carefully and guessing the result.

We'll cover the following...

Puzzle code

Carefully read the code given below:

C++
#include <iostream>
struct Degrees
{
Degrees() : degrees_(0)
{
std::cout << "Default constructer invoked\n";
}
Degrees(double degrees) : degrees_(degrees)
{
std::cout << "Constructed with " << degrees_ << "\n";
}
double degrees_;
};
struct Position
{
Position() : latitude_{1} { longitude_ = Degrees{2}; }
Degrees latitude_;
Degrees longitude_;
};
int main()
{
Position position;
}

Your task: Guess the output

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

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

All Cards
1
2
3
Missing Cards
(Drag and drop the cards in the blank spaces)

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