VOOZH about

URL: https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-enum

⇱ What is C++ Enum, And How to Use Enums in C++ [2024 Edition]


HomeResourcesSoftware DevelopmentC++ Tutorial for BeginnerWhat is C++ Enum, And How to Use Enums in C++
Tutorial Playlist
C++ Tutorial for BeginnerOverview
The Easiest Guide to Understand and Learn C++ BasicsLesson - 1All You Need to Know About Classes in C++Lesson - 2The Difference Between C++ and CLesson - 3The Ultimate Guide to Learn About C++ EnumLesson - 4An Easy Guide To Understand The C++ ArrayLesson - 5The Best Guide to C++ For Loop : For Loops Made EasyLesson - 6Everything You Need to Know About C++ VectorLesson - 7The Easiest Way to Understand C++ FunctionsLesson - 8Call by Value and Call by Reference in C++Lesson - 9The Ultimate Step-by-Step Guide to Learn Pointers in C++Lesson - 10The Complete Guide to Learn the Difference Between Coding Vs ProgrammingLesson - 11C++ Object-Oriented Programming : The Best Way to Learn C++ OopsLesson - 12The Supreme Guide to Build the Tic Tac Toe Game in C++Lesson - 13The Best Guide to Create the C++ GUI ApplicationLesson - 14C++ vs. Python: Everything You Need to KnowLesson - 15What Is Sorting in C++: Bubble Sort, Insertion Sort & MoreLesson - 16The Best Explanation to Break and Continue Statements in C++Lesson - 17The Best Guide to Understand C++ Header FilesLesson - 18C++ Recursion: Understanding Its Concept and WorkingLesson - 19Top 60 C++ Interview Questions and Answers for 2026Lesson - 20C# Vs. Java: Which Is the Best Programming Language?Lesson - 21The Ultimate Guide to Learn C++ STL (Standard Template Library)Lesson - 22The Top 10 Reasons to Learn C++ Language in 2026Lesson - 23C++ Projects for Beginners : The Best Article for Beginner-Level ProjectsLesson - 24

What is C++ Enum, And How to Use Enums in C++

Lesson 4 of 24By Ravikiran A S

Last updated on Oct 9, 2024234330

Table of Contents

View More

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 way to define and group integral constants. It also makes the code easy to maintain and less complex. In this tutorial, you will learn about C++ enum in detail.

Why Do You Use Enums?

Enums or enumerations are generally used when you expect the variable to select one value from the possible set of values. It increases the abstraction and enables you to focus more on values rather than worrying about how to store them. It is also helpful for code documentation and readability purposes.

Want a Top Software Development Job? Start Here!AI-Powered Full Stack Developer ProgramExplore Program

What Is C++ Enum?

Enum, which is also known as enumeration, is a user-defined data type that enables you to create a new data type that has a fixed range of possible values, and the variable can select one value from the set of values. For example, suppose you are the owner of an ice cream shop, and you sell a limited range of ice cream flavors. So you want the customers to select only from that collection of ice creams in your shop. This becomes an enumeration with ice cream as the name of enumeration and different flavors of ice creams as its elements.

πŸ‘ Enum_Example1.

Now you will have a look at the definition and declaration of the enum.

Definition and Declaration of Enum

To define enum in C++, you must use the enum keyword along with the elements separated by commas. The basic syntax of enum is:

Syntax:

πŸ‘ Enum_Example2

Here, enum is the keyword that instructs the code, the name of the enum is the name that will be assigned to enum, and Element 1, Element 2, …., Element 5 are the values assigned to the enumeration.

Want a Top Software Development Job? Start Here!AI-Powered Full Stack Developer ProgramExplore Program

Example:

πŸ‘ Enum_Example3.

Each of these elements has values starting from 0, like vanilla is 0, chocolate is 1, etc. The default values mentioned can be altered during the declaration of the enum in C++.

Declaration

To declare an enum variable, write the name of the enumeration along with the enum variable.

πŸ‘ Enum_Example4

Here i is the enum variable.

Example:

πŸ‘ Enum_Example5

In the above example, you declared enumeration with elements as the name of players, and one captain is to be selected from these players. You declared the enum variable cap in the int main and assigned Avi to the enum variable. Inside the if statement, if Avi's value is equal to the cap, only then the if block executes.

Inside the block, you must print Avi, but it will print Avi's default value, that is 0.

πŸ‘ Enum_Example6

Now have a look at how you can change the default values of enums.

πŸ‘ Enum_Example9

In the above example, you can see that you assigned values to the elements, i.e. cars. You assigned two elements, i.e. Valkyrie, whose value is 240 to the speed1 enum variable, and Roadster, whose value is 250 to the enum variable speed2.

Now, using the comparison operator inside the if statement we are concluding if speed 2 is greater, then the if block executes that means it’s a win for Roadster. Otherwise, the else block executes, which means it’s a win for Valkyrie. 

Now, you will look into C++ enums with flags.

Our Free Courses with Certificate

Introduction to C++Enroll Now!
Learn Advanced C++ CourseEnroll Now!
C Programming BasicsEnroll Now!
Introduction to C#Enroll Now
JavaScript for BeginnersEnroll Now!

Enums With Flags

Suppose you want to buy a mobile with a large storage capacity and better camera quality. 

πŸ‘ Enum_Example7.

As you can see, you assigned values 1, 2, 4 to the BatteryCapacity, CameraQuality, and LargeStorage, respectively. You assigned these integer values with a power of 2 to avoid overlapping. This is because you want to use two flags at once, i.e. CameraQuality and LargeStorage. Therefore, the power of 2 with the integer values will not let overlapping occur while using the bitwise operator.

You have set the flags CameraQuality and LargeStorage because you want a mobile with both features.

πŸ‘ Enum_Example8

Output 6 indicates both the flags are used.

Want a Top Software Development Job? Start Here!AI-Powered Full Stack Developer ProgramExplore Program

Conclusion

After reading this article on C++ enum, you would have learned why to use enums and what is enum in C++, and their definition and declaration. You also learned Enums with flags and how you can change the default values of the elements.

C++ is one of the top programming languages to learn. If you are looking for a more comprehensive learning experience that is designed to help you instance scale up and grown as Full stack software development professional, our Full Stack Developer - MERN Stack is what you should enroll for. It has all the right ingredients  you need to build a full stack career the right way.

On the other hand, if you have any questions on this article on C++ enum, do not hesitate to share them by writing to us in the comments section. We’ll get our SMEs to review and resolve your queries soon. To get a deeper understanding of C++ enum, you could also click on the following link: C++ enum and explore the tutorial video.

About the Author

πŸ‘ Ravikiran A S
Ravikiran A S

Ravikiran A S is a Technical Content Strategist and Data Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More

Recommended Programs

πŸ‘ Full Stack Java Developer Masters Program

Full Stack Java Developer Masters Program

1029 Learners
Lifetime Access*
πŸ‘ AI-Powered Full Stack Developer Program

AI-Powered Full Stack Developer Program

1594 Learners
Lifetime Access*
πŸ‘ Java Certification Training

Java Certification Training

16365 Learners

*Lifetime access to high-quality, self-paced e-learning content.

Explore Category
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.
  • *All trademarks are the property of their respective owners and their inclusion does not imply endorsement or affiliation.
  • Career Impact Results vary based on experience and numerous factors.