C++ enum sample code

Spread the love

enum colors{ red, green, blue};

std::cout << “Red ” << red << ” Green ” << green << ” Blue ” << blue << “\n”;

enum is short for enumeration and in this process a list is assigned numerical values, each item in the list, one more than the item before it.


If one arbitrarily assigns a value of 4 to an enum item, the next item will be 5

// Example program
#include <iostream>
#include <string>

enum colors{ red, green, blue};
enum shops{ Amazon=5, Walmart, Shoppers=10, Walgreen, Disney };
int main()
{
    
    std::cout << "Red: " << red  << " Green: " << green << " Blue: " << blue << std::endl;
    std::cout << "Amazon: " << Amazon  << " Walmart: " << Walmart << " Shoppers: " 
              << Shoppers << " Walgreen: " << Walgreen << " Disney: "<< Disney << std::endl;
}


Note in the above code, we set Amazon=5

Note also the restarting of the enumeration from that integer level going forward.


Top