Enum In C++ Programming


Enumeration ( Keyword of enum ) is a user defined data type in C++.It consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex. They using for the Switch case. The switch statement is c++ multi-way branch statement. It is used to take the place of long if-else chains, and make them more readable. However, unlike if statements, one may not use inequalities; each value must be concretely defined.

The program demonstrates the usage of an enumerated data type in C++. An enumerated type is a user-defined data type that consists of a set of named constants.

  • In this program, we define an enumeration type gender with two values, Male and Female.
  • The program then declares a variable g of type gender and initializes it with the Male value.
  • A switch statement is used to check the value of the g variable. If g has the value Male, the program outputs the string "Gender Male". If g has the value Female, the program outputs the string "Gender Female". If g has any other value, the program outputs "Invalid".
  • Finally, the main function returns 0 to indicate successful program execution.

Source Code

#include<iostream>
using namespace std;
//  ENUM in C++
 
enum gender {Male,Female};
int main()
{
    gender g=Male;
    switch(g)
    {
    case Male:
        cout<<"Gender Male";
        break;
    case Female:
        cout<<"Gender Female";
        break;
    default:
         cout<<" Invalid";
        break;
 
    }
    return 0;
}
 

Output

Gender Male
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples