How to define an enumerated type (enum) in C++?


An enumerated type declares an optional type name and a set of zero or more identifiers that can be used as values of the type. Each enumerator is a constant whose type is the enumeration. For example, if you are creating an application that has a fixed number of types for some variable. For example, let's say gender, it can be of three types male, female and others. You can define and use an enum like −

#include<iostream>
using namespace std;
enum Gender {MALE, FEMALE, OTHERS};
int main() {
   Gender gen = Gender.FEMALE;
   return 0;
}

By default, the value of the first name is 0, the second name has the value 1, and the third has the value 2, and so on. But these values don't matter because enums are basically used to have a symbolic meaning. Whenever you compare an enum in your program, you'd be just using its symbolic meaning and not its actual value.

Updated on: 11-Feb-2020

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements