enum class in C++

Level 4226 keystrokes

Declares a strongly typed enum class Color and uses a switch to print the name of the selected value, Green.

C++ code

// Scoped enum: values must be written as Color::Red and do not convert to int implicitly
1enum class Color { Red, Green, Blue };
2
// Pick the Green value
3Color c = Color::Green;
4
// Branch on the enum value
5switch (c) {
6 case Color::Red: cout << "Red" << endl; break;
// This case matches and prints Green; break leaves the switch
7 case Color::Green: cout << "Green" << endl; break;
8 case Color::Blue: cout << "Blue" << endl; break;
9}

Output

Green
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 12
  • :× 11
  • ;× 8
  • "× 6
  • ,× 2
  • {× 2
  • }× 2
  • (× 1
  • )× 1
  • =× 1

More C++ examples at level 4

← C++ typing practice