Search Your Topic

config
Thursday, 15 June 2017 06:06

C++ Enumeration

Written by
Rate this item
(0 votes)

Enum in C++ is a data type that contains fixed set of constants.

It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The C++ enum constants are static and final implicitly.

C++ Enums can be thought of as classes that have fixed set of constants.


Points to remember for C++ Enum

  • enum improves type safety
  • enum can be easily used in switch
  • enum can be traversed
  • enum can have fields, constructors and methods
  • enum may implement many interfaces but cannot extend any class because it internally extends Enum class

C++ Enumeration Example

Let's see the simple example of enum data type used in C++ program.

 
  1. #include <iostream>  
  2. using namespace std;  
  3. enum week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };  
  4. int main()  
  5. {  
  6.     week day;  
  7.     day = Friday;  
  8.     cout << "Day: " << day+1<<endl;  
  9.     return 0;  
  10. }     

Output:

Day: 5

 

Read 26 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « C++ Structs C++ friend function »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.