codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

31 / 87 topics
28Structures (struct)29Structure and Function30Pointers to Structure31Enumerations (enum)32Unions
Tutorials/C++ Programming/Enumerations (enum)
⚡C++ Programming

Enumerations (enum)

Updated 2026-05-12
30 min read

Enumerations (enum)

Enumerations, commonly known as enums, are a user-defined data type in C++ that groups related constants together. They provide a way to assign names to integral constants, making the code more readable and maintainable. Enums are particularly useful when you have a set of related values that represent different states or categories.

In this tutorial, we'll explore traditional enums, scoped enums (using enum class), specifying underlying types for enums, and using enums in switch statements. Understanding these concepts will help you write cleaner and more efficient C++ code.

Introduction

Enums allow you to define a set of named constants that represent specific values. This makes your code more readable and easier to manage, especially when dealing with a fixed set of related values. For example, instead of using magic numbers like 0, 1, and 2 to represent days of the week, you can use Monday, Tuesday, and Wednesday.

Enums are particularly useful in scenarios where you need to define a set of constants that are logically related but don't require complex operations. By using enums, you can make your code more expressive and less prone to errors.

Traditional Enums

A traditional enum is defined using the enum keyword followed by the name of the enum and a list of named constants enclosed in curly braces {}. Each constant is assigned an integer value starting from 0 by default, but you can explicitly assign values if needed.

Example: Basic Enum

C++
1// basic_enum.cpp
2#include <iostream>
3
4enum Day {
5 Monday,
6 Tuesday,
7 Wednesday,
8 Thursday,
9 Friday,
10 Saturday,
11 Sunday
12};
13
14int main() {
15 Day today = Wednesday;
16 std::cout << "Today is day number " << today << std::endl; // Output: Today is day number 2
17 return 0;
18}
Output

Here, we explicitly assign values to each constant in the Color enum. This can be useful when you need specific integer values for your constants.

Enum Class (Scoped Enums)

Traditional enums have some limitations, such as polluting the global namespace and allowing implicit conversions between enums and integers. To address these issues, C++11 introduced scoped enums using the enum class keyword. Scoped enums are defined within a scope, which prevents name clashes and requires explicit conversion when used.

Example: Basic Enum Class

C++
1// basic_enum_class.cpp
2#include <iostream>
3
4enum class Weekday {
5 Monday,
6 Tuesday,
7 Wednesday,
8 Thursday,
9 Friday,
10 Saturday,
11 Sunday
12};
13
14int main() {
15 Weekday today = Weekday::Wednesday;
16 std::cout << "Today is day number " << static_cast<int>(today) << std::endl; // Output: Today is day number 2
17 return 0;
18}
Output

Here, we define a scoped enum Color with explicit values. Accessing and using these constants follows the same rules as in the previous example.

Underlying Type of Enums

By default, enums use an integer type (int) to store their values. However, you can specify a different underlying type for an enum using the : TYPE syntax after the enum name. This can be useful when you want to save memory or ensure specific behavior.

Example: Enum with Specified Underlying Type

C++
1// specified_underlying_type.cpp
2#include <iostream>
3
4enum class Status : char {
5 Active = 'A',
6 Inactive = 'I'
7};
8
9int main() {
10 Status userStatus = Status::Active;
11 std::cout << "User status is: " << static_cast<char>(userStatus) << std::endl; // Output: User status is: A
12 return 0;
13}
Output

In this example, the Direction enum is used in a switch statement to determine the heading based on its value. This makes the code more readable and easier to manage.

Practical Example

Let's create a complete program that uses enums to represent different types of fruits and their corresponding nutritional values. We'll use both traditional enums and scoped enums, and demonstrate how to use them in a switch statement.

C++
1// fruit_nutrition.cpp
2#include <iostream>
3
4enum Fruit {
5 Apple,
6 Banana,
7 Orange
8};
9
10enum class Nutrient {
11 Calories = 100,
12 Fiber = 5,
13 VitaminC = 12
14};
15
16int main() {
17 Fruit myFruit = Banana;
18 Nutrient nutrientType = Nutrient::Calories;
19
20 std::cout << "My favorite fruit is: ";
21 switch (myFruit) {
22 case Apple:
23 std::cout << "Apple" << std::endl;
24 break;
25 case Banana:
26 std::cout << "Banana" << std::endl;
27 break;
28 case Orange:
29 std::cout << "Orange" << std::endl;
30 break;
31 default:
32 std::cout << "Unknown fruit" << std::endl;
33 break;
34 }
35
36 std::cout << "Nutrient type: ";
37 switch (nutrientType) {
38 case Nutrient::Calories:
39 std::cout << "Calories" << std::endl;
40 break;
41 case Nutrient::Fiber:
42 std::cout << "Fiber" << std::endl;
43 break;
44 case Nutrient::VitaminC:
45 std::cout << "Vitamin C" << std::endl;
46 break;
47 }
48
49 return 0;
50}
Output
My favorite fruit is: Banana
Nutrient type: Calories

In this program, we define two enums: Fruit for different types of fruits and Nutrient (a scoped enum) for different nutritional values. We then use these enums in switch statements to display information about the selected fruit and nutrient.

Summary

  • Traditional Enums: Define a set of named constants using the enum keyword. They are unscoped and can pollute the global namespace.
  • Enum Class (Scoped Enums): Defined using enum class. They are scoped, preventing name clashes and requiring explicit conversions.
  • Underlying Type: You can specify the underlying type for an enum using the : TYPE syntax.
  • Enums in Switch Statements: Enums are often used in switch statements to handle different cases based on specific values.

What's Next?

In the next tutorial, we'll explore unions, which allow you to store different data types in the same memory location. Unions are useful when you need to save memory and ensure that only one of several variables is active at any given time. Stay tuned!


PreviousPointers to StructureNext Unions

Recommended Gear

Pointers to StructureUnions