Core C++

Enums Program 2

Overview

In this C++ example, we demonstrate how to use an enum to define a type that could be used to represent color values.
 

Example

#include <iostream>

// We can define an enum to represent colors.
// In this case, keRed = 0, keBlue = 1, keYellow = 2.
enum EColor {keRed, keBlue, keYellow};

int main() {
    using namespace std;

    // Create an instance of the type and assign it the value keRed.
    EColor eMyColor = keRed;

    cout << "Color enum value = " << eMyColor << endl;

    // Output the constant enum values.
    cout << "keRed = " << keRed << endl;
    cout << "keBlue = " << keBlue << endl;
    cout << "keYellow = " << keYellow << endl;

    // Keep the console window open.
    cin.get();
    return 0;
}

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.