In this lecture you will learn about the enumerations in C plus plus.
So find a way to provide the constants is to use an enum value.
So in effect, an enum is a group of named constants, which means that you can use an enum as a parameter
to function.
So for example, let's call the.
A cards here.
Uh, clubs.
Simons.
Parts.
Space.
Actually, it's not cards.
Let's sweets here.
Sweets.
So this defines an enumeration called Sweets with named values for the sweets in a deck of cards.
So an enumeration is an integer type, and by default, the compiler will assume an integer.
But you can change this by specifying the integer type in the declaration.
So since there are just four possible values for cards sweets, it's a waste memory to use integer usually
four bytes, and instead we can use a character as a single byte.
So let's make a character sweets here and car double dots here.
Car clubs.
Here.
Diamonds.
Here shirts.
Here and spades.
Space.
Yeah.
So as you can see, we have a sweets here.
Sweets character.
Let's name it.
And as you can see here, enumeration value club.
So we have this same value.
We can't have same values in here.
So my clubs.
Here.
My diamonds.
Here, My hearts and my spades.
So when you use an enumerated value, you can use just the name.
However, it's usual to scope it with the name of an enumeration so it will make the code more readable.
So, for example, Swift's card card one equals diamonds.
Swift's card two equals Swift's, for example.
Spades.
Space.
Here.
So both forms are allowed.
They are the same, but the latter makes it more explicit that the value is taken from an enumeration.
So to force developers specify the scope.
You can apply the keyword class here in animal in after in here, for example, class.
As you can see here, we put the class here.
We now obligated to add and mention the enum type before we choose this.
Uh, declared values.
So, as you can see, we got an error here.
If we delete the class, these both quotes will work in this example.
So actually, I will add comment as this so you can test it in your computer class and here.
So let's compile our application, see if we have any errors.
As you can see, we don't have any errors here.
So with this definition, um, here, this part to the line declaring car two will compile, but the
line declaring card one as you can see here, will not.
So.
With the scope scoped in here.
The compiler treats the enumeration as a new type and has no inbuilt conversion for your new type to
an integer variable.
For example, let's create an A here.
Character.
Actually, let's comment this for now.
And the comment is to.
Character C equals card plus ten.
As you can see here, this is the undeclared identifier card.
When we compile this card was not declared in this scope.
Do you did you mean card to let's start card two?
And as you can see here, we get an error two because we just want to use card card here.
So in order to do this.
As you can see here, sweets cart here.
Sweets cart equals sweets.
Diamonds.
Here.
And see card plus ten.
So we get an error here.
As you can see here.
Now, I will explain why we got an error here.
So that's because.
The enum type is based on character, but when you define the switch variable as being scoped with class
here, as you can see here, the second line will not compile if the enumeration is defined as not being
scoped without class here.
Let's without class then there is an inbuilt conversion between the enumerated value and character here.
Oops.
So let's select class.
And as you can see, we got no errors anymore and our code compiles successfully.
So by default, the compiler will give the first enumerator a value of zero.
So and then increment the value by the subsequent enumeration enumerators.
So use the sweets diamonds here so it's diamonds will have value one because it's the second value in
sweets enum here so you can assign values yourself to.
So for example, let's create another enumeration type enum car brands and car brands.
Okay, so then let's add.
So for example, BMW, BMW, let's make it a 100 M or Mercedes.
Mercedes here, 200, for example, Fiat here.
300.
Your Volkswagen.
400.
And.
Let's make another car name.
Here.
Um, I forgot the card names here.
Um.
So rain out here, 999 or 900, 999 here, make it that way.
So in this case.
The BMW has value of 700.
The BMW has value of 100.
Mercedes 200.
Fiat 300.
Volkswagen 400 and renowned 9999.
And in this case here, as you can see, we assign values so there are no longer no longer this.
For example, my class equals zero.
And as you can see, this value is one.
As you can see here, value is.
Three two here, as you can see.
And value is nine three here.
So when you select this, you will see the value of 909,999.
Here.
As you can see here.
Uh, value is 400.
As you can see here.
So enumerations are useful in a switch statement, which you will see later in this course because the
named value makes it clearer than using just an integer.
So you can also use an enumeration as a parameter to to a function and hence restrict the values based
via parameters.
So now I want to create a function with it.
For example, void stack and.
You can pass parameters, for example, as car brands, suites and other named enumerations.
So in next lecture, we're going to declare we're going to go depth into the pointers and namespaces.
So I'm waiting you in next lecture.