Enums in C++

  1. What are enums

    An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. It makes your code easier to read and maintain. You use enums when you have values that arent going to change. Values in enum must be integers.

  2. Creating enums

    To create enum you use enum keyword and name

    enum directions {
        N = 0, S = 1, W = 2, E = 3
    };

    You then create an enum variable just like declaring object from class.

     directions dir;

    You can then asign a value to it. Value must be defined in enum.

     dir = N;

    You can also set type of an enum using :

     enum directions : unsigned int{
         N = 0, S = 1, W = 2, E = 3
     }