How to create an enumeration of constants with and without explicit values in C++

1 Answer

0 votes
/* 
   Title: Enumeration of Constants in C++ 
   Example with and without explicit values 
*/

#include <iostream>

using std::cout;
using std::endl;

// Enum WITHOUT explicit values
enum Color {
    Red,      // 0
    Green,    // 1
    Blue      // 2
};

// Enum WITH explicit and mixed values
enum Status {
    OK = 1,       // 1
    Warning = 5,  // 5
    Error,        // 6 (auto: previous + 1)
    Critical = 10 // 10
};

int main() {
    cout << "Enum without explicit values:" << endl;
    cout << "Red = " << Red << endl;
    cout << "Green = " << Green << endl;
    cout << "Blue = " << Blue << endl;

    cout << "\nEnum with explicit and mixed values:" << endl;
    cout << "OK = " << OK << endl;
    cout << "Warning = " << Warning << endl;
    cout << "Error = " << Error << endl;
    cout << "Critical = " << Critical << endl;
}


/* 
run:

Enum without explicit values:
Red = 0
Green = 1
Blue = 2

Enum with explicit and mixed values:
OK = 1
Warning = 5
Error = 6
Critical = 10

*/

 



answered Apr 25 by avibootz

Related questions

...