What are enumerated data types in C++?

C++ProgrammingObject Oriented Programming

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.

raja
Published on 16-Feb-2018 10:25:27
Advertisements