How to create an enumerated collection in C++

1 Answer

0 votes
#include <iostream>

// Define an enumeration
enum Alphabet {
    A = 1, // Assign 1 to 'A'
    B,     // Automatically assigned 2
    C,     // Automatically assigned 3
    D      // Automatically assigned 4
};

int main() {
    // Access enumeration values
    Alphabet letter = B; // Assign 'B' (value 2) to the variable

    // Print the integer value of the enum
    std::cout << "The value of B is: " << letter << std::endl;

    std::cout << "A = " << A << ", B = " << B << ", C = " << C << ", D = " << D << std::endl;
}

 
 
/*
run:
 
The value of B is: 2
A = 1, B = 2, C = 3, D = 4
 
*/

 



answered Aug 29, 2025 by avibootz
...