Loading...

C++ Enums

The polymorphism page left Tag pure virtual, Box printing ok, Cap printing pine, and two Tag* calls from box.cpp. This page stays in the Inheritance module on purpose as its last core tool, even though the new work is a typed list, not another virtual override. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work names enums: a small typed list of constant values. Write enum class Mark with Ok and Pine. Select Mark::Ok in main. Print ok when the selected mark equals Mark::Ok. Do not keep the Tag, Box, or Cap classes in the checked file. Debugging waits for the next page.
An enum class is a named type whose values are a fixed list you write once. Reading enum class Mark is reading that type name. Reading Ok and Pine inside the braces is reading the allowed marks. Reading Mark m = Mark::Ok; is reading one selected value. Reading the if that prints ok is reading the branch that matches the selected mark. Keep #include <iostream> so the print works. Do not invent a debugging tool on this page.
Programs use enums when a value should be one of a few named choices instead of a free string or a magic number. You do not need inheritance classes here. You do not need a new workshop file name. The build command stays familiar: one source file box.cpp, one output name box, one run that prints ok. You compile only box.cpp again for this workshop file on disk.
Think of an enum as a sticky-note list of allowed stamps. The previous page showed two children under one call. This page shows two named marks under one type. Do not expect debugging in the checked file. The printed ok through Mark::Ok is the whole lesson point.

Above main, write enum class Mark with Ok and Pine inside the braces. Inside main, write Mark m = Mark::Ok;, then an if that checks m == Mark::Ok and prints ok, then return 0;. Keep #include <iostream> at the top. Do not keep Tag, Box, or Cap. Do not invent a debugging demo on this page.
Walk the run carefully from the first line of main. The program starts at main. Mark m = Mark::Ok; stores the selected mark. The if compares that mark to Mark::Ok. Because they match, the body prints ok. Then main returns and the program ends. There is one printed line on the screen: ok. The checked file never builds the old inheritance classes.
Say the steps out loud once while you read the file. Write enum class Mark with Ok and Pine. Select Mark::Ok. Compare and print ok. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No Tag pointer. No Cap class. When the screen shows ok, the story matched the source.

If you write Mark m = Pine; without the Mark:: prefix, the build fails because enum class values are scoped. That failed experiment stays in the prose. The checked file uses Mark::Ok.
If you select Mark::Pine instead and still only test for Mark::Ok, the screen stays empty. The checked file selects Mark::Ok so ok appears.
A common mistake is to treat the enum like a string and wait for cin. Another common mistake is to invent the word debugging as a required keyword here. A third mistake is to leave leftover Tag or Cap classes in the file. A fourth mistake is to print pine while Mark::Ok is selected. A fifth mistake is to drop class and confuse this page with an unscoped demo you are not using.
This page stops before the next tool, where you learn to find mistakes carefully in a small program. The next page names that tool once at the end. The enum you have just written only shows a typed list and one selected mark that prints ok. Keep the printed ok so the order stays clear for a first careful reading of the file.
Remember that enum class keeps the mark names under the type. You still need a selected value and a branch to print. After a successful run the screen shows ok. After you undo a bad experiment the checked file returns to that Mark::Ok path. Stability of the checked file matters as much as learning the enum spelling itself.

Open box.cpp. Remove the Tag, Box, and Cap classes from the polymorphism page. Write enum class Mark { Ok, Pine }; above main. Inside main, write Mark m = Mark::Ok; and print ok when the mark equals Mark::Ok. Do not add a debugger call. The program file name on disk stays box.cpp. The checked file is below, and the new mark list is the enum.

box.cpp enums enum class Mark Ok Pine

cpp
#include <iostream>

enum class Mark
{
    Ok,
    Pine
};

int main()
{
    Mark m = Mark::Ok;
    if (m == Mark::Ok)
    {
        std::cout << "ok" << std::endl;
    }
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows ok.
Check the printed ok first. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the build fails about an undeclared identifier, you forgot the Mark:: scope. If the screen is empty, you selected Mark::Pine or the if never matched. If old Tag code remains, remove it before you trust the enum path.

📊 enums Mark and what the screen shows

Piece What the screen shows
First ok, because Mark::Ok matches the if
Last ok, because the program prints once and returns
Rule enum class Mark lists Ok and Pine; Mark::Ok selects the printed path
Screen ok

Try one change at a time. Temporarily set Mark m = Mark::Pine;, rebuild, and see an empty screen while the if still tests for Mark::Ok. Restore Mark::Ok. Next, temporarily print ready inside the if, rebuild, see ready, then restore ok. Each experiment changes one thing on purpose, so you see either no print or a different print. Undo each change before the next one starts, so the file always returns to the checked workshop version with Mark::Ok and ok once again after every single careful try back on disk again.
This page ends the enums tool and stops before the next one. The next page is debugging, which names careful checking of a small program when a result looks wrong. The enum you have just written only shows enum class Mark with Ok and Pine from box.cpp while Mark::Ok still prints ok. You leave with ok on the screen. Count the printed lines before you leave. There is exactly one printed line on that final screen, and that line is ok alone right there now after the final successful build run on disk today now after the enum class check.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top