1 00:00:00,460 --> 00:00:05,350 In this lecture you will learn about the enumerations in C plus plus. 2 00:00:05,350 --> 00:00:10,930 So find a way to provide the constants is to use an enum value. 3 00:00:10,930 --> 00:00:19,150 So in effect, an enum is a group of named constants, which means that you can use an enum as a parameter 4 00:00:19,150 --> 00:00:19,990 to function. 5 00:00:19,990 --> 00:00:22,840 So for example, let's call the. 6 00:00:24,380 --> 00:00:25,910 A cards here. 7 00:00:26,890 --> 00:00:28,060 Uh, clubs. 8 00:00:29,050 --> 00:00:29,740 Simons. 9 00:00:30,890 --> 00:00:31,340 Parts. 10 00:00:32,600 --> 00:00:33,430 Space. 11 00:00:33,680 --> 00:00:36,650 Actually, it's not cards. 12 00:00:36,680 --> 00:00:37,760 Let's sweets here. 13 00:00:37,790 --> 00:00:38,330 Sweets. 14 00:00:38,570 --> 00:00:48,020 So this defines an enumeration called Sweets with named values for the sweets in a deck of cards. 15 00:00:48,020 --> 00:00:54,920 So an enumeration is an integer type, and by default, the compiler will assume an integer. 16 00:00:54,920 --> 00:00:59,510 But you can change this by specifying the integer type in the declaration. 17 00:00:59,510 --> 00:01:06,560 So since there are just four possible values for cards sweets, it's a waste memory to use integer usually 18 00:01:06,560 --> 00:01:11,780 four bytes, and instead we can use a character as a single byte. 19 00:01:11,780 --> 00:01:19,910 So let's make a character sweets here and car double dots here. 20 00:01:20,030 --> 00:01:21,680 Car clubs. 21 00:01:22,590 --> 00:01:23,250 Here. 22 00:01:23,670 --> 00:01:24,690 Diamonds. 23 00:01:25,660 --> 00:01:27,490 Here shirts. 24 00:01:29,140 --> 00:01:31,660 Here and spades. 25 00:01:32,950 --> 00:01:33,510 Space. 26 00:01:33,620 --> 00:01:33,930 Yeah. 27 00:01:33,940 --> 00:01:36,340 So as you can see, we have a sweets here. 28 00:01:36,380 --> 00:01:38,740 Sweets character. 29 00:01:39,830 --> 00:01:40,360 Let's name it. 30 00:01:40,360 --> 00:01:44,010 And as you can see here, enumeration value club. 31 00:01:44,020 --> 00:01:46,240 So we have this same value. 32 00:01:46,270 --> 00:01:49,270 We can't have same values in here. 33 00:01:49,270 --> 00:01:52,180 So my clubs. 34 00:01:53,010 --> 00:01:53,450 Here. 35 00:01:53,910 --> 00:01:56,580 My diamonds. 36 00:01:57,300 --> 00:02:02,220 Here, My hearts and my spades. 37 00:02:02,910 --> 00:02:08,580 So when you use an enumerated value, you can use just the name. 38 00:02:08,580 --> 00:02:16,260 However, it's usual to scope it with the name of an enumeration so it will make the code more readable. 39 00:02:16,260 --> 00:02:22,770 So, for example, Swift's card card one equals diamonds. 40 00:02:24,450 --> 00:02:31,200 Swift's card two equals Swift's, for example. 41 00:02:31,200 --> 00:02:32,010 Spades. 42 00:02:33,320 --> 00:02:34,040 Space. 43 00:02:35,200 --> 00:02:35,710 Here. 44 00:02:35,890 --> 00:02:38,620 So both forms are allowed. 45 00:02:38,650 --> 00:02:45,850 They are the same, but the latter makes it more explicit that the value is taken from an enumeration. 46 00:02:45,850 --> 00:02:48,490 So to force developers specify the scope. 47 00:02:48,490 --> 00:02:59,290 You can apply the keyword class here in animal in after in here, for example, class. 48 00:02:59,710 --> 00:03:02,980 As you can see here, we put the class here. 49 00:03:03,670 --> 00:03:13,240 We now obligated to add and mention the enum type before we choose this. 50 00:03:14,590 --> 00:03:16,000 Uh, declared values. 51 00:03:16,240 --> 00:03:18,610 So, as you can see, we got an error here. 52 00:03:18,610 --> 00:03:24,010 If we delete the class, these both quotes will work in this example. 53 00:03:24,460 --> 00:03:32,350 So actually, I will add comment as this so you can test it in your computer class and here. 54 00:03:32,350 --> 00:03:36,880 So let's compile our application, see if we have any errors. 55 00:03:36,880 --> 00:03:39,760 As you can see, we don't have any errors here. 56 00:03:39,760 --> 00:03:51,430 So with this definition, um, here, this part to the line declaring car two will compile, but the 57 00:03:51,430 --> 00:03:57,310 line declaring card one as you can see here, will not. 58 00:03:57,310 --> 00:03:58,180 So. 59 00:03:59,380 --> 00:04:02,170 With the scope scoped in here. 60 00:04:02,200 --> 00:04:09,670 The compiler treats the enumeration as a new type and has no inbuilt conversion for your new type to 61 00:04:09,670 --> 00:04:10,880 an integer variable. 62 00:04:10,900 --> 00:04:14,320 For example, let's create an A here. 63 00:04:17,270 --> 00:04:18,170 Character. 64 00:04:19,580 --> 00:04:22,430 Actually, let's comment this for now. 65 00:04:22,940 --> 00:04:25,160 And the comment is to. 66 00:04:26,480 --> 00:04:30,290 Character C equals card plus ten. 67 00:04:33,010 --> 00:04:36,340 As you can see here, this is the undeclared identifier card. 68 00:04:36,370 --> 00:04:40,720 When we compile this card was not declared in this scope. 69 00:04:40,870 --> 00:04:45,280 Do you did you mean card to let's start card two? 70 00:04:45,430 --> 00:04:52,420 And as you can see here, we get an error two because we just want to use card card here. 71 00:04:52,450 --> 00:04:55,330 So in order to do this. 72 00:04:56,480 --> 00:05:01,730 As you can see here, sweets cart here. 73 00:05:02,300 --> 00:05:05,780 Sweets cart equals sweets. 74 00:05:05,810 --> 00:05:06,770 Diamonds. 75 00:05:08,290 --> 00:05:08,620 Here. 76 00:05:09,930 --> 00:05:13,170 And see card plus ten. 77 00:05:13,170 --> 00:05:14,940 So we get an error here. 78 00:05:16,120 --> 00:05:17,190 As you can see here. 79 00:05:17,200 --> 00:05:20,290 Now, I will explain why we got an error here. 80 00:05:20,290 --> 00:05:22,270 So that's because. 81 00:05:23,070 --> 00:05:31,100 The enum type is based on character, but when you define the switch variable as being scoped with class 82 00:05:31,110 --> 00:05:38,880 here, as you can see here, the second line will not compile if the enumeration is defined as not being 83 00:05:39,000 --> 00:05:41,130 scoped without class here. 84 00:05:41,490 --> 00:05:50,250 Let's without class then there is an inbuilt conversion between the enumerated value and character here. 85 00:05:50,580 --> 00:05:50,900 Oops. 86 00:05:50,970 --> 00:05:52,860 So let's select class. 87 00:05:52,860 --> 00:05:58,620 And as you can see, we got no errors anymore and our code compiles successfully. 88 00:05:58,620 --> 00:06:04,380 So by default, the compiler will give the first enumerator a value of zero. 89 00:06:04,920 --> 00:06:09,720 So and then increment the value by the subsequent enumeration enumerators. 90 00:06:09,720 --> 00:06:21,930 So use the sweets diamonds here so it's diamonds will have value one because it's the second value in 91 00:06:22,350 --> 00:06:27,640 sweets enum here so you can assign values yourself to. 92 00:06:27,670 --> 00:06:36,520 So for example, let's create another enumeration type enum car brands and car brands. 93 00:06:37,270 --> 00:06:40,060 Okay, so then let's add. 94 00:06:40,240 --> 00:06:51,040 So for example, BMW, BMW, let's make it a 100 M or Mercedes. 95 00:06:51,370 --> 00:06:59,650 Mercedes here, 200, for example, Fiat here. 96 00:07:00,860 --> 00:07:01,790 300. 97 00:07:01,820 --> 00:07:03,140 Your Volkswagen. 98 00:07:04,280 --> 00:07:05,240 400. 99 00:07:06,130 --> 00:07:06,850 And. 100 00:07:08,700 --> 00:07:11,370 Let's make another car name. 101 00:07:15,090 --> 00:07:15,720 Here. 102 00:07:16,900 --> 00:07:19,600 Um, I forgot the card names here. 103 00:07:20,320 --> 00:07:20,650 Um. 104 00:07:24,470 --> 00:07:31,610 So rain out here, 999 or 900, 999 here, make it that way. 105 00:07:32,030 --> 00:07:34,550 So in this case. 106 00:07:35,400 --> 00:07:39,060 The BMW has value of 700. 107 00:07:39,420 --> 00:07:42,300 The BMW has value of 100. 108 00:07:42,330 --> 00:07:44,100 Mercedes 200. 109 00:07:44,130 --> 00:07:46,050 Fiat 300. 110 00:07:46,080 --> 00:07:50,610 Volkswagen 400 and renowned 9999. 111 00:07:51,030 --> 00:07:58,740 And in this case here, as you can see, we assign values so there are no longer no longer this. 112 00:07:58,740 --> 00:08:02,340 For example, my class equals zero. 113 00:08:02,340 --> 00:08:05,910 And as you can see, this value is one. 114 00:08:05,910 --> 00:08:09,900 As you can see here, value is. 115 00:08:11,490 --> 00:08:14,100 Three two here, as you can see. 116 00:08:14,100 --> 00:08:18,270 And value is nine three here. 117 00:08:18,270 --> 00:08:26,940 So when you select this, you will see the value of 909,999. 118 00:08:28,780 --> 00:08:29,260 Here. 119 00:08:31,450 --> 00:08:32,620 As you can see here. 120 00:08:33,390 --> 00:08:36,180 Uh, value is 400. 121 00:08:36,210 --> 00:08:37,840 As you can see here. 122 00:08:37,860 --> 00:08:46,530 So enumerations are useful in a switch statement, which you will see later in this course because the 123 00:08:46,530 --> 00:08:49,980 named value makes it clearer than using just an integer. 124 00:08:49,980 --> 00:08:57,300 So you can also use an enumeration as a parameter to to a function and hence restrict the values based 125 00:08:57,330 --> 00:08:59,010 via parameters. 126 00:08:59,010 --> 00:09:02,910 So now I want to create a function with it. 127 00:09:03,390 --> 00:09:07,140 For example, void stack and. 128 00:09:08,860 --> 00:09:16,330 You can pass parameters, for example, as car brands, suites and other named enumerations. 129 00:09:16,330 --> 00:09:24,370 So in next lecture, we're going to declare we're going to go depth into the pointers and namespaces. 130 00:09:24,370 --> 00:09:26,920 So I'm waiting you in next lecture.