We’re slowly working our way towards classes and objects

Slides:



Advertisements
Similar presentations
C Programming Lecture 23 Enumeration Types Structures.
Advertisements

Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Variables – the Key to Programming. ICS-3M1 - Mr. Martens - Variables Part 1 What is a Variable? A storage location that is given a “name” You can store.
6.1 Probability exercises. Def Def: If S is a finite sample space of equally likely outcomes, and E is an event, that is, a subset of S, then the probability.
Overview of Previous Lesson(s) Over View  Operator Overloading:  Operator overloading is a very important capability, it enables to make standard C++
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
1 EPSII 59:006 Spring HW’s and Solutions on WebCT.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Enumerated Types Mr. Dave Clausen La Cañada High School.
1 Introduction to Object Oriented Programming Chapter 10.
Overview of Previous Lesson(s) Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs,
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
GCSE COMPUTER SCIENCE Practical Programming using Python
Repetition Structures
11 Chapter Structured Data
© 2016, Mike Murach & Associates, Inc.
Introduction to Python
Random Sampling Playing cards are our participants
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
An Introduction to Functions
Functions CIS 40 – Introduction to Programming in Python
C Short Overview Lembit Jürimägi.
JavaScript Functions.
Enumerated DATA Types Enum Types Enumerated Data Types
8 Pointers.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Stacks, Queues, and Deques
Editable Attributes (1)
Variables ICS2O.
Fundamentals of Programming
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix.
Coding Concepts (Sub- Programs)
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 11: Structured Data.
Java Programming Arrays
Encryption and Decryption
Topic 1: Problem Solving
Fundamentals of Data Structures
CSCE 489- Problem Solving Programming Strategies Spring 2018
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
Topic 1: Problem Solving
Variables Title slide variables.
Repetition Structures
Coding Concepts (Data- Types)
Enumerated DATA Types Enum Types Enumerated Data Types
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2001 Bin Sort, Radix.
LONG MULTIPLICATION is just multiplying two numbers.
Designing a class.
Section 12.2 Theoretical Probability
Java Programming Language
Section 12.2 Theoretical Probability
Standard Version of Starting Out with C++, 4th Edition
CISC101 Reminders Assignment 3 due today.
Section 12.2 Theoretical Probability
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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

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

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

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

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

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?

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

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

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

Enumerations (2) When we run this we can see the new values we used in the console

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

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

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

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

Structs We then have structs They act like classes, in that they can have properties functions They can even use inheritance!

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

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

Structs (1) We can create a struct using the struct keyword Then give it a name

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

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

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

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

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

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

Struct Practice (2) Ex Once created, output each card to the console In the form “Jack of Hearts” See the example output

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

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

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

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

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

END OF UNIT!