10.x — Chapter 10 summary and quiz

📘 Chapter 10⏱ about 2 min read✍️ Beginner friendly
Read one box at a time. No rush.

Quick review#

  • enum class gives names to a fixed set of options.
  • A struct groups values into one type. Use . to reach members.
  • A class holds data and functions. An object is one made from it.
  • public = anyone can use. private = only the class itself.
  • A constructor sets up a new object. Use the member initializer list.
  • Mark read-only member functions const.
Quick check

Pick an answer. You find out right away if it is right.

Quick check #1

What is an object?

Quick check #2

Who can use private members of a class?

Quick check #3

When does a constructor run?

Quiz time
Question #1

Make an enum class called Direction with the options up, down, left and right.

C++
enum class Direction{    up,    down,    left,    right,};
Question #2

Make a struct Point with two double members, x and y. Then write a function that takes two points and returns the distance between them. (Distance = square root of (dx² + dy²).)

C++
#include <cmath> struct Point{    double x {};    double y {};}; double distance(const Point& a, const Point& b){    double dx { b.x - a.x };    double dy { b.y - a.y };    return std::sqrt(dx * dx + dy * dy);}
Question #3

Write a class Timer with a private int for seconds, a constructor that takes the starting seconds, a tick() function that takes away one second (but never below zero), and a bool isDone() const.

C++
class Timer{public:    Timer(int seconds)        : m_seconds { seconds }    {    }     void tick()    {        if (m_seconds > 0)            --m_seconds;    }     bool isDone() const    {        return m_seconds == 0;    } private:    int m_seconds {};};
Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.