Enumerated Types or Enums in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to understand Enumerated types or Enums in C++.

Enumerated types are user-defined data types in which the user can specify a limited number of values that can be allocated to the variable.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //defining enum variable
   enum Gender {
      Male,
      Female
   };
   Gender gender = Male;
   switch (gender) {
      case Male:
         cout << "Gender is Male";
         break;
      case Female:
         cout << "Gender is Female";
         break;
      default:
         cout << "Value can be Male or Female";
   }
   return 0;
}

Output

Gender is Male
raja
Published on 23-Mar-2020 08:13:59
Advertisements