Module 12: Enumerations in C++
An enumeration (enum) is a user-defined data type that represents a fixed set of named constant values. Enumerations make programs easier to read and maintain by replacing numeric constants with meaningful names.
For example, instead of using 0, 1, or 2 to represent days or colors, you can use descriptive names like Monday, Tuesday, or Wednesday.
1. enum
A traditional enum defines a group of named integer constants.
Syntax
1enum EnumName 2{ 3 VALUE1, 4 VALUE2, 5 VALUE3 6};
Example
1#include <iostream> 2 3using namespace std; 4 5enum Day 6{ 7 Monday, 8 Tuesday, 9 Wednesday, 10 Thursday, 11 Friday 12}; 13 14int main() 15{ 16 Day today = Wednesday; 17 18 cout << today; 19 20 return 0; 21}
Output
12
By default, enumeration values start from
0.
Assigning Custom Values
You can assign custom integer values to enumeration constants.
Example
1#include <iostream> 2 3using namespace std; 4 5enum Status 6{ 7 Success = 200, 8 NotFound = 404, 9 ServerError = 500 10}; 11 12int main() 13{ 14 cout << Success; 15 16 return 0; 17}
Output
1200
2. enum class
enum class was introduced in C++11. It provides strong type safety and prevents name conflicts.
Syntax
1enum class EnumName 2{ 3 VALUE1, 4 VALUE2 5};
Example
1#include <iostream> 2 3using namespace std; 4 5enum class Color 6{ 7 Red, 8 Green, 9 Blue 10}; 11 12int main() 13{ 14 Color color = Color::Green; 15 16 if (color == Color::Green) 17 { 18 cout << "Green Selected"; 19 } 20 21 return 0; 22}
Output
1Green Selected
3. Scoped Enumeration
A scoped enumeration is another name for enum class. Its members must be accessed using the scope resolution operator (::).
Example
1#include <iostream> 2 3using namespace std; 4 5enum class Direction 6{ 7 North, 8 South, 9 East, 10 West 11}; 12 13int main() 14{ 15 Direction dir = Direction::North; 16 17 if (dir == Direction::North) 18 { 19 cout << "Moving North"; 20 } 21 22 return 0; 23}
Output
1Moving North
enum vs enum class
enum | enum class |
|---|---|
Implicitly converts to int | No implicit conversion |
| Members are in the surrounding scope | Members are scoped |
| Less type-safe | Strongly type-safe |
| Can cause name conflicts | Prevents name conflicts |
| Available in older C++ versions | Introduced in C++11 |
Best Practices
- Use
enum classin modern C++ projects. - Choose meaningful names for enumeration values.
- Assign custom values only when required.
- Use scoped enumerations to avoid naming conflicts.
- Group related constants into a single enumeration.
Practice Problems
- Create an
enumfor the days of the week. - Create an
enumfor months of the year. - Assign custom values to an enumeration.
- Use
enum classfor traffic light colors. - Write a program that prints different messages based on an enumeration value.
Interview Questions
- What is an enumeration in C++?
- What are the advantages of using
enum? - What is the difference between
enumandenum class? - What is a scoped enumeration?
- Why is
enum classconsidered safer thanenum? - Can you assign custom values to enumeration constants?
- When should you use
enum classinstead ofenum?
Summary
In this module, you learned:
- What enumerations are and how they improve code readability.
- How to create and use traditional
enum. - How to assign custom values to enumeration constants.
- How
enum classprovides strong type safety and scoped members. - The differences between
enumandenum class. - Best practices for using enumerations in modern C++ applications.
Next Module: Module 13: Object-Oriented Programming (OOP) — Classes, Objects, Constructors, Destructors, Encapsulation, Inheritance, Polymorphism, Abstraction, Virtual Functions, Friend Functions, and Operator Overloading.