Teaching Newbies since 2014

kauress

  • Home
  • Javascript ❤
    • JavaScript
    • Node.js
  • WebDev Tuts
  • screencasts
  • Resources
  • VR & AR
  • Contact
  • Github
  • Twitter
  • YouTube
  • RSS
  • C++
You are here: Home / C++ / Enumerated types (Enum) in C++

January 10, 2020 by: Kauress

Enumerated types (Enum) in C++

Enum is a user defined data type in C++ whose value is equal to some user defined values. For example:
//use enum keyword
// name of enum variable is "week"
#include <iostream>
using namespace std;

enum week { Moonday, Tuesdayz, WednesOhday, ThursdayNow, Fridayay, Saturdayikes, Sundayeez }; 
int main(){
   week my_day;
   my_day = WednesOhday; 
   cout<<  my_day;   
   return 0;
}
Enum is useful for switch cases, when we expect a variable to have only one of the possible set of values. For example, we can change the code to:
#include <iostream>
using namespace std;

enum week { Moonday, Tuesdayz, WednesOhday, ThursdayNow, Fridayay, Saturdayikes, Sundayeez }; 
int main(){
   week my_day;
   my_day = week::WednesOhday; 
   if(my_day == week::WednesOhday){
   cout<<  "It's WednesOhday";  
   } else {
   cout << "Not WednesOhday!";
   } 
   return 0;
}
Adding/pushing data to a vector in C++
A* Search Algorithm in C++

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2021 ·Kauress