Download presentation
Presentation is loading. Please wait.
Published byBambang Agusalim Modified over 6 years ago
2
We’re slowly working our way towards classes and objects
More Data Structures We’re slowly working our way towards classes and objects Before that, we have two more data structures to look at Enumerations Structs
3
Enumerations Enumerations let us create integral constants with names
Also known as enums Useful for creating finite sates or values in a program Such as the state of an enemy Calm, Alert, Chasing Can then use branching to change AI based off state
4
Enumerations (1) We define enumerations using the enum keyword
Followed by a name for that enum We can then enter in lots of names As many as we want For the possible values Both the name of the enum and the values inside them should use first-capital-letters
5
Use the name of the enum as the data-type of the variable
Enumerations (1) We can then create a variable That stores one of the enum’s values Use the name of the enum as the data-type of the variable
6
We use one of the values in the enum
Enumerations (1) We use one of the values in the enum As the value for the variable That’s all we do! We type in the name of the value We don’t type in the name of the enum
7
Enumerations (1) Ex Create a new empty C++ solution
Add the main() function to it Make it return 0 Create an enum for the suits of a deck of cards Add one value for each suit Output each of these values to the console What values are output?
8
When we output the values of the enum, we saw integers
Enumerations (2) When we output the values of the enum, we saw integers Starting at 0 Going up to 3 We mentioned this, but enum values are integral constants
9
Enumerations (2) A constant is a value that can’t change
The values in an enum have a set integer value they ‘represent’ We can change the value they ‘represent’ convert between integers and enum values
10
We can only use integer values
Enumerations (2) Let’s look at changing the integer value they represent Really easy to do Add an equal sign after the enum value Then enter what we want it to be We can only use integer values
11
Enumerations (2) When we run this we can see the new values we used in the console
12
Enumerations (2) Although we’re free to change these values, we need to think carefully About two things Why we want to change them (the default values often work fine) What to change them to These will depend on the program we’re making
13
Enumerations (2) Ex Change your enum values so they use different integer values Make sure they are output to the console Run your program and check that they now use the new values
14
Enumerations (3) We mentioned that we can convert between enum values and integers From enum to integer is implicit We only need to store the enum value in an integer variable From integer to enum value is explicit We need to run a function to handle the conversion
15
Enumerations (3) We have to use the static_cast function
Uses angular brackets Contain the data-type to convert to Then circular brackets Contain the value to convert
16
Structs We then have structs
They act like classes, in that they can have properties functions They can even use inheritance!
17
Structs While there is not much difference between a class and a struct, there is a difference in practice We should only use structs for “Plain-Old Data” types That only contain data And don’t contain functions This is a suggested use, and not a requirement
18
For example, we could use a struct to represent a ‘Card’
Structs For example, we could use a struct to represent a ‘Card’ It has a suit It has a value But doesn’t do anything
19
Structs (1) We can create a struct using the struct keyword Then give it a name
20
We don’t have to give them values
Structs (1) We can give this struct variables Where specific data is stored We simply make variables as per norm We don’t have to give them values Or use public/private
21
Create a struct for a ‘Card’
Structs (1) Ex Create a struct for a ‘Card’ Make it outside any scope Give it two variables A Suit A value
22
Structs (2) We can then make a variable using this struct type
We don’t give it a value Simply declaring the variable is enough We won’t get null pointer exceptions
23
Structs (2) From here, we can give the struct values
By accessing the variables in it Note that we can’t output a struct as-is We need to output their individual variables
24
Structs (2) Ex Try creating a Card value in the main() Give this struct values for the Suit and value Output this struct to the console
25
Struct Practice (1) Ex To get some practice with structs, create the following program Create an array of Card values (52, to be exact) Use some clever maths to give each card The correct suit (you may need to use conversion here) The correct value
26
Struct Practice (2) Ex Once created, output each card to the console
In the form “Jack of Hearts” See the example output
27
Create a separate function that shuffles the array of Cards
Struct Practice (3) Ex Create a separate function that shuffles the array of Cards Run the function, and output the array of cards afterwards To make sure it is shuffled
28
Struct Practice (4) Ex Create a function that sorts the cards in a specific order First in order of suit (Hearts Diamonds Clubs Spades) Then in order of value Run this function on your array after shuffling it Then output it Make sure they are in the correct order now
29
Struct Practice (5) Ex Make a function for playing a game of Red or Black Takes an array of Cards Starts by showing the first Card in the array Then asks user if the next one is red or black If they get it right, they get a point and move on to the next card If they get it wrong, output their score and end the function
30
Make a struct that represents a Point in 2D space
Struct Practice (6) Ex Make a struct that represents a Point in 2D space With x and y coordinates Make a function that takes two structs And calculates the distance between them Use Pythagoras’ Theorem to do this
31
Struct Practice (7) Ex Make a function that takes two parameters
An array of Points A single Point This function should return the shortest distance found Between the lone Point And the Points in the array
32
END OF UNIT!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.