
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
enum Class in C++
An enumeration, or enum in C++, is a user-defined data type formed by a set of named integral constants. However, enum types from tradition have the disadvantage of potential name clashes and the absence of type safety.
To solve this problem, C++ introduced the concept of enum class, which is also referred to as scoped enumerations.
Declaring an enum Class
This is the following syntax of declaring an enum class, which is done by using scoped enumeration (enum class).
enum class EnumName { Value1, Value2, Value3, // ... other enumerator values. };
Here, EnumName is the name of the enumeration class.
Value1, Value2.. is the named value(constants) inside the enum class.
Accessing Values from an enum Class
You can access the values of an enum class by using the Scope Resolution Operator (::) with the enum class name. Consider the following syntax:
EnumName::Value
Here, EnumName is the name of the enum class. Value is one of the enumerators defined in the class.
Example of Enum Class
Here is the following example for the enum class in C++.
#include <iostream> using namespace std; // Declare an enum class for days of the week enum class Day { Monday, // 0 Tuesday, // 1 Wednesday, // 2 Thursday, // 3 Friday, // 4 Saturday, // 5 Sunday // 6 }; int main() { Day today = Day::Friday; if (today == Day::Friday) { cout << "Today is Friday!" << endl; } return 0; }
Output
Today is Friday!
Explanation
- First, we declared an enum class named Day with their values.
- And then set the variable today to Day::Friday, and then we compared it to Day::Friday to print a message.
Underlying Type of an Enum Class
The enum class uses an integer as its default underlying type but many more underlying types are possible: unsigned int, char, uint8_t, int16_t, etc.
Default Underlying Type of enum class
When you declare the enum class without specifying any underlying type, then by default it is set to int. This means that each enumerator's value will be stored as an int (4 bytes).
As we already see in the above following example.
Specifying a Custom Underlying Type for an enum class
Here, you can explicitly specify the underlying type of an enum class by adding a colon (:) after the enum class name. This will allow for control of the storage size and range of values.
Syntax
Here is the following syntax for it.
enum class EnumName : UnderlyingType { Value1, Value2, Value3 };
Here, UnderlyingType could be any integral type (like unsigned int, char, uint8_t, etc.).
Example of Underlying Type of an Enum Class
Here is the following example using unsigned int as Underlying Type.
#include <iostream> using namespace std; enum class Status : unsigned int { Ok = 0, // 0 Error = 1, // 1 Warning = 2 // 2 }; int main() { Status s = Status::Error; // Print the integer value of the enum cout << "The integer value of Error is: " << static_cast<unsigned int>(s) << endl; return 0; }
Output
The integer value of Error is: 1