Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Defining and Using Enums | Enumerations in C++
Quizzes & Challenges
Quizzes
Challenges
/
C++ Structures and Enumerations

bookDefining and Using Enums

Note
Definition

An enum in C++ is a user-defined type that assigns names to integer constants, making code more readable and meaningful.

Enumerations are useful when you need to represent a fixed set of related values, such as directions, days, or states. By using named constants instead of raw numbers, you make your code easier to understand and maintain. Enums are declared with the enum keyword, followed by a name and a list of values inside curly braces.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> enum Color { Red, Green, Blue }; int main() { Color favorite = Green; if (favorite == Green) std::cout << "Your favorite color is Green." << std::endl; else std::cout << "Your favorite color is not Green." << std::endl; // Assigning another value favorite = Blue; if (favorite == Blue) std::cout << "Now your favorite color is Blue." << std::endl; }

Using enums in your code makes it much clearer what values are valid for a particular variable:

  • Enums restrict variables to a defined set of named options;
  • You avoid assigning plain integers, which could be set to any value;
  • This reduces the risk of bugs caused by invalid values;
  • Code readability is improved, because you see descriptive names instead of numbers.

It is much easier to understand favorite = Green; than favorite = 1; when reading or debugging your programs.

question mark

Which statement best describes the purpose of an enum in C++

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you show me an example of how to declare and use an enum?

What programming languages support enums?

Are there any limitations or drawbacks to using enums?

Awesome!

Completion rate improved to 8.33

bookDefining and Using Enums

Scorri per mostrare il menu

Note
Definition

An enum in C++ is a user-defined type that assigns names to integer constants, making code more readable and meaningful.

Enumerations are useful when you need to represent a fixed set of related values, such as directions, days, or states. By using named constants instead of raw numbers, you make your code easier to understand and maintain. Enums are declared with the enum keyword, followed by a name and a list of values inside curly braces.

main.cpp

main.cpp

copy
12345678910111213141516171819202122
#include <iostream> enum Color { Red, Green, Blue }; int main() { Color favorite = Green; if (favorite == Green) std::cout << "Your favorite color is Green." << std::endl; else std::cout << "Your favorite color is not Green." << std::endl; // Assigning another value favorite = Blue; if (favorite == Blue) std::cout << "Now your favorite color is Blue." << std::endl; }

Using enums in your code makes it much clearer what values are valid for a particular variable:

  • Enums restrict variables to a defined set of named options;
  • You avoid assigning plain integers, which could be set to any value;
  • This reduces the risk of bugs caused by invalid values;
  • Code readability is improved, because you see descriptive names instead of numbers.

It is much easier to understand favorite = Green; than favorite = 1; when reading or debugging your programs.

question mark

Which statement best describes the purpose of an enum in C++

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 3. Capitolo 1
some-alt