Enums

Learn about Enums and how they offer an efficient way to handle predefined values in your code
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
3D art showing a fantasy character
Ryan McCombe
Ryan McCombe
Updated

Another common data requirement we will soon run into is a desire to have a property be one of a limited set of values.

We've seen booleans for capturing one of two states - true or false, but what if we want a property to be one of three, four, or more possibilities?

We could imagine a need to store a character's faction, for example, with possible values being "Goblin", "Undead", and "Troll"

Defining Enums

This is where the concept of an enumerated type is useful. These are sometimes called enumerations, or simply enums. We create an enum in C++ like this:

enum class Faction {
  Goblin,
  Undead,
  Troll,
  Dragon
};
Test your Knowledge

Creating Enums

How can we create an enum called Mood?

Using Enums

Variables that store one of these values will have a type of Faction, and we can access the options using the scope resolution operator ::. Three examples of this are shown below:

Faction EnemyType { Faction::Dragon };
bool isTroll(Faction SelectedFaction) {
  return SelectedFaction == Faction::Troll;
}
class Vampire {
public:
  Faction GetFaction() { return mFaction; }
private:
  Faction mFaction { Faction::Undead }
};

Vampire Enemy;
Faction EnemyFaction { Enemy.GetFaction() };
Test your Knowledge

Using Enums

How can use our Mood enum to add a variable to the Character class?

enum class Mood { Friendly, Neutral, Hostile };

class Character {
  // ?
};

Advantages of Enums

We could have captured these multiple-choice variables as some other type - for example, integers or strings.

But, if we use integers, our code becomes quite difficult to follow, as we have to remember what the numbers mean. For example, is 3 a troll or a dragon?

Strings like "Dragon" solve the readability problem. However, they consume more memory and network resources than an int, and they're also slower to compare.

Comparing Strings

When our software asks a computer to compare two integers, for example, 3 == 3, that can be done in a single operation.

Comparing strings is a bit more involved, as potentially every character needs to be compared, one by one. A request like "Dwarf" == "Dwarf" would typically require 6 checks - one for each character, and an additional check to ensure there are no further characters.

Enums are fast to compare because, behind the scenes, they're just stored as integers by default.

The enum syntax is simply some compiler syntax to let us solve this problem by attaching some semantic meaning to a collection of integers.

Enums combine the performance of integers with the readability of strings. Additionally, they have even more advantages:

  • The compiler will only permit valid values - there's no risk we introduce a bug by mistyping a string, or using a number that doesn't correspond to any value.
  • Because an enum defines what the possible options are, our tools can be helpful. For example, our code editor will generally provide auto-complete support when we're working with enums. As soon as we type Faction::, our IDE will likely display the range of Faction values available to us.

Summary

This lesson introduced the concept of Enums in C++. Enums allow us to represent a set of predefined values, enhancing both code readability and efficiency.

Key Learnings:

  • Enums allow for defining a variable to be one of a limited set of predefined values, like Faction with options such as Goblin, Undead, and Troll.
  • The enum class syntax provides a strongly typed enumeration.
  • Enums are more efficient and readable than using integers or strings for multiple-choice variables.
  • The scope resolution operator :: is used to access enum values.
  • Enums, being stored as integers internally, are fast to compare and help prevent errors compared to strings or plain integers.

Preview of the Next Lesson

In our upcoming lesson, we'll dive into the use of using statements, allowing us to simplify our code and enhance readability.

Key Topics to Be Covered:

  • Understanding the using statement and its role in simplifying code.
  • Application of using statements in namespaces to avoid namespace pollution.
  • How to use using statements with enums to streamline code readability and usage.
  • Utilizing using statements for type aliases.
  • Best practices and common pitfalls when implementing using statements in various C++ programming scenarios.

Was this lesson useful?

Next Lesson

The using Keyword

This lesson introduces the using keyword in C++, focusing on namespaces, enums, and type aliasing
3D art showing a fantasy character
Ryan McCombe
Ryan McCombe
Updated
Lesson Contents

Enums

Learn about Enums and how they offer an efficient way to handle predefined values in your code

3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, Unlimited Access
Namespaces and Enums
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 56 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Next Lesson

The using Keyword

This lesson introduces the using keyword in C++, focusing on namespaces, enums, and type aliasing
3D art showing a fantasy character
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved