C++ Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 





Enum Datatype in C++


Tutorials Cpp

Topic

How are Enum / Enumerated Datatype used in C++?



Explanation

Enum Datatype consist of a set of named values. These values can be used in indexing expressions.

The idea behind enumerated datatype is to create new data types that can take on only a restricted range of values.

Syntax:
   enum enum-type-name { enumeration list } variable-list
Example:

#include <iostream.h>
int main()
  {
     enum Fruits{orange, guava, apple};
     Fruits myFruit;
     int i;
     cout << "Please enter the fruit of your choice(0 to 2)::";
     cin >> i;
     switch(i) 
       {
         case orange:
         cout << "Your fruit is orange";
         break;
         case guava:
         cout << "Your fruit is guava";
         break;
         case apple:
         cout << "Your fruit is apple";
         break;
      }
     return 0;
  }

Result:
    Please enter the fruit of your choice(0 to 2)::2
    Your fruit is apple

In the above example only the variable "i" is an integer. But using the enumeration the named values are used in a "switch case" statement with integer value "i".

This is how enum or enumerated datatype used in C++.








Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting