Technology5 min read

Enums in C++

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

Enumeration in C++

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a means to declare and arrange necessary constants. It also makes the code easy to maintain and less complex. In this lesson, you will learn about C++ enum in depth.

Enumeration in C++

Enum is a user-defined data type where we declare a set of values for a variable, and the variable can only accept one out of a small group of potential deals. Enum, also known as enumeration, is a user-defined data type that enables you to build a new data type with a specified range of potential values. The variable can select one value from the set of possibilities. For example, say you are the proprietor of an ice cream business, and you serve a restricted choice of ice cream flavours. So you want the customers to select solely from that variety of ice creams in your shop. This becomes an enumeration with ice cream as the name of enumeration and several types of ice creams as its elements. We use the enum keyword to define an Enumeration.

enum direction {East, West, North, South}dir;


Here Enumeration name in the direction which can only accept one of the four stated values, the dir after the declaration is an enum variable.

Let us take a basic example to explain this:

Here we have assigned the value West to the enum variable dir, and when we presented the importance of dir, it shows 1. By default, the values are increasing order starting from 0, which means East is 0, West is 1, North is 2, and South is 3.

#include<iostream>
using namespace std;
enum direction {East, West, North, South}dir;
int main()
{
   dir = West;
   cout<<dir;
   return 0;
}

As seen in the above example, we have stated the enum variable dir during the enum declaration. There is another approach to declare an enum variable.

#include <iostream>
using namespace std;
enum direction {East, West, North, South};
int main(){
   direction dir;
   dir = South; 
   cout<<dir;   
   return 0;
}

Why Use Enum in C++

Now that we understand what is an enum and how to use them in the program, let us explore why we use them:
Enums are used only when we expect the variable to have one of the possible sets of values. For example, we have a dir variable that holds the direction. Since we have four movements, this variable can take any one of the four values. If we try to assign a different random value to this variable, it will throw a compilation error. This boosts compile-time checks and avoids problems by sending in erroneous constants.

Another critical place where they are used frequently is switch-case statements, where all the values that case blocks expect can be defined in an enum. This way, we can assure that the enum variable that we pass in switch parenthesis is not taking any random value that it shouldn’t accept.

Enums or enumerations are often used when you anticipate the variable to select one value from the potential set of values. It raises the abstraction and helps you focus more on matters rather than thinking about how to store them. It is also vital for code description and readability considerations.

How to Alter Default Values of Enum

#include <iostream>
using namespace std;
enum direction {East=11, West=22, North=33, South=44};
int main(){
   direction dir;
   dir = South;
   cout<<dir; 
   return 0;
}

Enum allocation and Forward Declaration

Enum types are considered part of the integer family of classes, and it’s up to the compiler to determine how much memory to allocate for an enum variable. The C++ standard stipulates that the enum size needs to be large enough to represent all enumerator values. It will typically make enum variables the same size as a regular int.

Because the compiler needs to know how much memory to allocate for an enumeration, you can only forward declare them when you also specify a fixed base. Because establishing an enumeration does not issue any memory, if an enumeration is needed in many files, it is appropriate to declare the enumeration in a header, and #include that header wherever required.

Rules and Regulation for Enum in C++

Below are a few of the rules and regulations for enum in C++.

  • It is advisable to use enum over macros because macros don’t have any data type & can override prior settings.
  • To represent a set of same-named constants, always use enumerations.
  • Instead of using plain “enum”, use “enum class” to avoid misunderstanding.
  • For safety and ease, always specify operations on enumerations.
  • For defining or utilizing enumerators, don’t use names in ALL CAPS.
  • Always name enumerations, don’t leave on default.
  • If necessary, then merely describe the underlying type of the enumeration.
  • Allocate values to enumerators only when it’s needed.

Advantages of Enum Data Types

  • Enum is constant rather than a number, and it assists in boosting the source code readability.
  • Enum also requires you to consider all the maximum potential values an enumerator can take in codes.
  • Enum permanently restricts the values an enum variable can accept in programming.

Conclusion

Enum is vital to use in programming, and it saves a lot of time and memory space which indirectly makes the code efficient and faster in terms of speed. Because when you define an enum, only a blueprint for the variable will be built, requiring no memory space.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next