0

So before learning c++, I am already quite experience with c# language. In c#, i was able to make an enumeration class that contains enum for the program. I was wondering, how to make an enumeration class in c++. I am using netbean 8.2 to write the code. What I mean for enum class is not an enum in a class but the whole class itself is enumeration.

Edit: I've managed to figure it. Thank you to everyone that helped.

  • Does this help: https://stackoverflow.com/questions/12183008/how-to-use-enums-in-c ? – Renat Nov 07 '19 at 09:25
  • There´s no such thing as an enumeration-class in neither C++ nor C#. You seem to have a simple class with a single member of type `enum`. – MakePeaceGreatAgain Nov 07 '19 at 09:25
  • Not sure if it's helpful, though if you need some extra functionality on top of enumerations, you can look at https://stackoverflow.com/a/57346836/2466431 – JVApen Nov 07 '19 at 10:15

2 Answers2

3

We can simply do this:

int main()
{
   enum class Color // "enum class" defines this as a scoped enumeration instead of a standard enumeration
   {
      RED, // RED is inside the scope of Color
      BLUE
   };

   enum class Language
   {
      ENGLISH, // ENGLISH is inside the scope of Language
      ITALIAN
   };

   Color color = Color::RED; // note: RED is not directly accessible any more, we   have to use Color::RED
   Language language = Language::ENGLISH; // note: ENGLISH is not directly accessible any more, we have to use Language::ENGLISH
}
cracker
  • 111
  • 4
0

An enumeration is a distinct type whose value is restricted to a range of values which may include several explicitly named constants ("enumerators"). The values of the constants are values of an integral type known as the underlying type of the enumeration. An enumeration is defined using the following syntax(c++):

 enum-key attr(optional) enum-name enum-base(optional) ; 

Each enumerator becomes a named constant of the enumeration's type (that is, name), visible in the enclosing scope, and can be used whenever constants are required.

enum Color { red, green, blue };
    Color r = red;
    switch(r)
    {
        case red  : std::cout << "red\n";   break;
        case green: std::cout << "green\n"; break;
        case blue : std::cout << "blue\n";  break;
    }

For more details: [https://en.cppreference.com/w/cpp/language/enum]

VJAYSLN
  • 473
  • 4
  • 12