1 00:00:05,370 --> 00:00:09,690 In this video, we'll look at the structure or syntax of an enumeration. 2 00:00:10,710 --> 00:00:15,540 If you've ever seen an enumeration before, you may be confused by the structure shown here. 3 00:00:15,810 --> 00:00:18,840 That's because many elements of an enumeration are optional. 4 00:00:19,200 --> 00:00:23,640 So often you'll see enumerations without names, types or attributes. 5 00:00:24,360 --> 00:00:27,510 You'll notice that we have also omitted attributes from this structure. 6 00:00:27,540 --> 00:00:33,450 That's because attributes are a separate topic altogether and apply to more than just enumerations in 7 00:00:33,450 --> 00:00:36,540 this section will only be covering the basics of enumerations. 8 00:00:36,540 --> 00:00:39,270 So topics such as attributes won't be included. 9 00:00:39,720 --> 00:00:44,370 With that being said, let's take a look at the elements of an enumeration that will be using. 10 00:00:45,780 --> 00:00:47,940 First we have the enumeration key. 11 00:00:47,970 --> 00:00:51,690 This defines the beginning of the enumeration as well as its scope. 12 00:00:51,720 --> 00:00:56,640 We'll cover the different types of enumeration scopes in detail soon, but for now, just know that 13 00:00:56,640 --> 00:01:01,410 the enumeration can be either scoped or scoped regardless of the scope. 14 00:01:01,410 --> 00:01:04,739 And enumeration will always start with the keyword enum. 15 00:01:05,430 --> 00:01:12,120 The next part of an enumeration is its name naming is optional for scoped enumerations but can be useful 16 00:01:12,120 --> 00:01:16,620 for imposing type restrictions on variables that you want to be able to enumeration type. 17 00:01:17,040 --> 00:01:22,530 This is similar to what we saw in the last video where we imposed a type restriction on the state variable 18 00:01:22,530 --> 00:01:25,390 by declaring it as a state enumeration type. 19 00:01:25,410 --> 00:01:28,960 If we had not named the state enumeration, we would not been able to do that. 20 00:01:28,980 --> 00:01:31,050 We'll see soon why this is the case. 21 00:01:31,530 --> 00:01:37,860 Next, we have a colon followed by a type specified where the specified type serves as the fixed underlying 22 00:01:37,890 --> 00:01:40,500 type of the enumerations enumerators. 23 00:01:40,800 --> 00:01:46,380 Specifying the underlying type is optional but can be used to reduce the amount of space and enumeration 24 00:01:46,380 --> 00:01:52,590 takes up in memory or to increase the accuracy of any calculations involving the enumerator values. 25 00:01:53,100 --> 00:01:58,770 The last part of the enumeration is a set of curly braces, and this is the Enumerations Enumerator 26 00:01:58,770 --> 00:01:59,370 List. 27 00:01:59,400 --> 00:02:05,100 This is where we define the names and integral values of the enumerators belonging to the enumeration. 28 00:02:05,130 --> 00:02:08,070 This can be done explicitly or implicitly. 29 00:02:08,100 --> 00:02:08,740 That's it. 30 00:02:08,759 --> 00:02:12,150 Now let's look at a simple enumeration to see these elements in action. 31 00:02:13,580 --> 00:02:16,640 This is about as simple of an enumeration as we can get. 32 00:02:16,940 --> 00:02:23,210 It has no name, no fixed underlying type, and has three enumerators red, green and blue. 33 00:02:23,630 --> 00:02:26,660 You'll notice that none of the enumerators have been initialized. 34 00:02:26,690 --> 00:02:32,030 This is because the compiler could do this for us if the first enumerator has no initialization. 35 00:02:32,270 --> 00:02:38,510 The compiler was signed at a value of zero for all other enumerators without initialize, as the compiler 36 00:02:38,510 --> 00:02:41,820 will assign the value of the previous enumerator plus one. 37 00:02:41,840 --> 00:02:44,840 This is known as implicit initialization. 38 00:02:45,290 --> 00:02:50,510 Of course, we might not always want the integral values assigned by the compiler, so we can always 39 00:02:50,510 --> 00:02:52,670 explicitly assign them ourselves. 40 00:02:52,940 --> 00:02:58,880 In some cases, we may want to assign certain enumerator specific values and not care what the others 41 00:02:58,880 --> 00:02:59,510 are assigned. 42 00:02:59,540 --> 00:03:05,030 In cases like these, we can explicitly assign certain enumerators the values we want, and have the 43 00:03:05,030 --> 00:03:07,790 compiler implicitly assign values to the others. 44 00:03:07,820 --> 00:03:11,930 This is known as explicit implicit initialization. 45 00:03:12,260 --> 00:03:16,100 Now let's take a look at the enumerations underlying type in more detail. 46 00:03:18,000 --> 00:03:23,910 If the underlying type of an enumeration is not fixed, the compiler assigns the first integral type 47 00:03:23,910 --> 00:03:27,390 that it's able to hold the enumerations entire value range. 48 00:03:27,420 --> 00:03:33,810 These integral types are listed on the right and rank order with signs given preference over unsigned. 49 00:03:34,020 --> 00:03:39,840 The sizes or widths of these integral types depends on the system being used, but for this example, 50 00:03:39,840 --> 00:03:44,460 we'll use the C++ minimum standard, which are reflected by the values listed. 51 00:03:44,970 --> 00:03:50,160 Now with this knowledge, can you tell what underlying type the compiler will assign to our simple enumeration? 52 00:03:51,260 --> 00:03:56,870 It might help to look at the minimum binary representation of our numerator values, assuming a signed 53 00:03:56,870 --> 00:03:57,890 bit is used. 54 00:03:58,130 --> 00:04:03,590 We could see that each of the numerators can be represented by three bits, and so the first integral 55 00:04:03,590 --> 00:04:08,720 type that is able to hold all three bits is INT, since it has a width of 16 bits. 56 00:04:09,110 --> 00:04:15,830 Now what if we explicitly initialize the numerator blue with the value negative three 2769? 57 00:04:16,430 --> 00:04:21,950 Obviously the underlying type will need to be signed since it must be able to hold negative values. 58 00:04:21,950 --> 00:04:25,220 But can we tell whether it will be an int a long or a long long? 59 00:04:25,460 --> 00:04:29,420 Let's take a look at the minimum binary representation of our numerator values. 60 00:04:29,420 --> 00:04:36,980 Again, the numerator is red and green, require three bit representation and blue a 32 bit representation 61 00:04:37,190 --> 00:04:41,020 because the underlying type must be able to hold all the numerator values. 62 00:04:41,030 --> 00:04:47,450 The compiler will base its selection on the largest required bit representation in this case blue. 63 00:04:47,480 --> 00:04:51,290 And so the compiler will assign the enumeration the underlying type of law. 64 00:04:52,440 --> 00:04:58,590 In some cases we may want to fix an enumerations underlying type for the purposes of saving memory or 65 00:04:58,590 --> 00:05:02,610 increasing the accuracy of calculations involving enumerator values. 66 00:05:03,240 --> 00:05:09,510 This can be accomplished by placing a colon and the fixed underlying data type directly before the Enumerators 67 00:05:09,510 --> 00:05:10,860 Enumeration list. 68 00:05:11,430 --> 00:05:18,900 C++ has types such as Unit eight, UT 16 and so forth that allow us to specify the exact number of bits 69 00:05:18,900 --> 00:05:19,950 that we require. 70 00:05:20,430 --> 00:05:25,270 If we try to fix an underlying type that cannot hold the enumerations entire range. 71 00:05:25,290 --> 00:05:26,910 The compiler will throw an error. 72 00:05:27,300 --> 00:05:30,600 Now let's look at the naming of an enumeration in more detail. 73 00:05:31,860 --> 00:05:36,240 Nameless enumerations are often referred to as anonymous enumerations. 74 00:05:36,600 --> 00:05:39,210 Because anonymous enumerations have no name. 75 00:05:39,210 --> 00:05:43,050 It's not possible to declare variables of that enumeration type. 76 00:05:43,750 --> 00:05:47,770 The best we can do is to clear a variable as the underlying type of the enumeration. 77 00:05:47,920 --> 00:05:52,690 But this means that while the variable can be assigned to the enumerators, it can also be assigned 78 00:05:52,690 --> 00:05:55,060 any value belonging to the underlying type. 79 00:05:55,450 --> 00:06:00,250 Because of this, we say that anonymous enumerations provide no type safety. 80 00:06:01,500 --> 00:06:06,210 In the case of the anonymous enumeration containing the numerators red, green and blue. 81 00:06:06,630 --> 00:06:12,390 If we want the variable, my color to be assigned one of these enumerators, we must declare it as the 82 00:06:12,390 --> 00:06:15,840 underlying type of the enumeration in this case int. 83 00:06:16,410 --> 00:06:21,720 But this means that while it can be assigned red, green and blue, it can also be assigned any integer 84 00:06:21,720 --> 00:06:22,290 value. 85 00:06:23,330 --> 00:06:29,120 Oftentimes we want the variables assignment to be restricted to only the numerators of an enumeration. 86 00:06:29,270 --> 00:06:31,950 As we saw earlier with the rocket launch example. 87 00:06:31,970 --> 00:06:33,710 This can be quite important. 88 00:06:33,950 --> 00:06:39,830 So to accomplish this, we can give an enumeration a meaningful name that represents its enumerators. 89 00:06:40,070 --> 00:06:45,860 By providing a name, we're defining the enumeration as a unique type that can only assume its enumerated 90 00:06:45,860 --> 00:06:46,610 values. 91 00:06:46,640 --> 00:06:50,630 Because of this, we say that the enumeration provides type safety. 92 00:06:51,500 --> 00:06:52,820 As good design practice. 93 00:06:52,820 --> 00:06:58,100 The name given to an enumeration should form an is a relationship with its enumerators. 94 00:06:58,130 --> 00:07:01,430 In this case, color is a good name for the enumeration. 95 00:07:01,430 --> 00:07:04,730 Since red is a color, green is a color and blue is a color. 96 00:07:05,240 --> 00:07:11,150 By giving the enumeration the name color, we have defined a unique type of that name, which can only 97 00:07:11,150 --> 00:07:13,580 assume the values of red, green and blue. 98 00:07:14,330 --> 00:07:20,270 As such, we can declare the variable my color as a color type, thus restricting its assignment to 99 00:07:20,270 --> 00:07:22,090 only red, green or blue. 100 00:07:22,100 --> 00:07:23,060 And that's it. 101 00:07:23,420 --> 00:07:26,570 Almost everything you need to know about an enumeration structure. 102 00:07:26,960 --> 00:07:30,910 The only thing that's left to cover other scopes and all the things we can do with them. 103 00:07:30,920 --> 00:07:36,050 Now let's head over to the next video to learn what scoped enumerations are and how we can use them.