In C++, enumeration is very important and widely used in different scenarios. In C++, enumeration can be done with the enum
keyword which can be used in unscoped and scoped enumerations. C++ is a great programming language that has many options to enumerate variables in a way which can make your code easier to read and understand. C++11 and above has modern enumeration methods that can be used with a good C++ Code Editor and compiler. In this post, we explain how to use enum
in unscoped enumeration in C++.
First of all, let’s learn what is enumeration and which enumeration types we can use in C++.
Table of Contents
What is enumeration and how to use enum C++?
An enumeration is used to declare constants in a range of values easily that it may include several explicitly named constants which are also called as enumerators. In C++, enumeration can be done with enum
keyword which is also known as an enum-key. The enum keyword can be used in two ways in C++. These are:
- Unscoped enumeration
- Scoped enumeration (Strongly Typed Enumeration)
In this article we explain how to use unscoped enumeration in C++, now lets see this.
Learn how to use enum in unscoped enumeration in C++
Unscoped enumeration is an old enumeration style, used before C++11 and improved after C++11 standards. In this enumeration type, the enum
keyword can be used to declare an unscoped enumeration type whose fundamental type is fixed, not fixed or can be used to specify the name and the type.
The unscoped enumerations can be used in 3 different syntax, these are,
enum
syntax for an unscoped enumeration type whose type is not fixed
1 |
enum name(optional) { enumarator = constexpr, enumarator = constexpr, ... } |
here is an example,
1 2 |
enum material { wood, metal, glass } ; // wood = 0, metal = 1, glass = 2 material t = wood; |
enum
syntax for an unscoped enumeration type whose type is fixed
1 |
enum name(optional) : type { enumarator = constexpr, enumarator = constexpr, ... } // unscoped enumeration type whose base type is not fixed |
here is an example,
1 2 |
enum dificulty:unsigned char { easy= 10, modarate = 20, hard = 30 } ; dificulty cpp = easy; |
enum
syntax for the unscoped enumeration type which is used to specify the name and the type.,
1 |
enum name : type ; |
here is an example,
1 |
enum mode:unsigned char; |
Is there a simple example of how to use enum in unscoped enumerations in C++?
Here is a simple example of how to use enum in unscoped enumerations in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> int main() { enum material { wood, metal, glass } ; material t = wood; std::cout << t ; system("pause"); return 0; } |
Is there a full example of how to use enum in unscoped enumeration in C++?
Here is a full example about how to use enum in unscoped enumeration in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> int main() { //unscoped enumeration type whose type is not fixed enum material { wood, metal, glass } ; material t = wood; switch(t) { case wood: std::cout << "wood material" << std::endl; break; case metal: std::cout << "metal material" << std::endl; break; case glass: std::cout << "glass material" << std::endl; break; } // unscoped enumeration type whose type is fixed (here unsigned char), enum dificulty:unsigned char { easy= 10, modarate = 20, hard = 30 } ; dificulty cpp = easy; // unscoped enumeration type which is used to specify the name and the type. enum mode:unsigned char; system("pause"); return 0; } |
These are the simple unscoped enumeration examples. In the scoped enumeration, we use struct or class as a scope for that enumeration. This is explained in another article in LearnCPlusPlus.org.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.