Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 15: User-Defined Types: enum and struct.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 25 Thanks for Lecture Slides: Dr. Sadaf Tanveer Dr. Sadaf Tanveer,
1 C++ Syntax and Semantics The Development Process.
Apostles Chess Club Lesson #4. Algebraic Chess Notation System The board is set up from white’s position. Black must look at the board from the white.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Introduction to Chess Naming the Chess Squares Names of the Pieces Initial Setup Materials taken from Teaching Chess Step By Step manuals, Books 1 and.
Dale/Weems/Headington
True or false A variable of type char can hold the value 301. ( F )
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 16: Classes and Objects.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
CS1061 C Programmuing Lecture 12 Arrays A. O’Riordan, 2004.
Data Types.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
1 CSE1301 Computer Programming Lecture 6: Components of a C Program (Part 2)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
1 CSE1020: Introduction to Computer Science I. 2 Today’s Lecture Course administrative details Material from Chapter 1 “Introduction to Programming” 19:00-22:00.
CPS120: Introduction to Computer Science
Chapter 8: Arrays.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Array, Structure and Union
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Primitive Variables.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
3-5 Working with Sets. Different Sets Roster Set: lists the elements of a set within braces, { } Example: the set of multiples of 2 {2, 4, 6, 8, …} Set-builder.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CHESS TRIVIA!!! By Alice Shirley Which is the only chess piece not able to move backwards? The King The Rook The Pawn The Bishop.
Chess By Kyle Fischer. What is chess? Chess is a game that you try to get the other person’s king in a checkmate.
Each piece is represented by a symbol. The pieces all stand in the same position at the start of the game the pieces are the Rook, the Knight, the Bishop,
CHESS. Write what you already know about chess on a piece of paper which I will hand out now.
CHESS. Write what you already know about chess on a piece of paper which I will hand out now.
1 CSC103: Introduction to Computer and Programming Lecture No 24.
© Jalal Kawash 2010 Elementary Set Theory Peeking into Computer Science.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Chapter One Lesson Three DATA TYPES ©
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
1 CS161 Introduction to Computer Science Topic #15.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
POORIA PANAHIFAR Mechanical Engineering Instructor: Dr. B. TAHERI The only thing that you have to do in order to understand what I’m saying is just sit.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Java Programming Language Lecture27- An Introduction.
Pointers, Enum, and Structures
CHESS PIECES.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
A Different Kind of Variable
CS150 Introduction to Computer Science 1
Module 2 Variables, Data Types and Arithmetic
Presentation transcript:

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 15: User-Defined Types: enum and struct

In This Lesson We Will: Learn how to enumerate possibilities Learn how to enumerate possibilities Learn to create an association between characteristics of an object that are of different data types Learn to create an association between characteristics of an object that are of different data types

Topic Enumerations

Enumerations Enumerable data types offer some important characteristics Enumerable data types offer some important characteristics Range is limited Range is limited Values are exact Values are exact The basis for all enumerations in C++ is the int data type The basis for all enumerations in C++ is the int data type Boolean and char Boolean and char User-defined User-defined

Example Imagine that you wish to model the pieces in a game of chess Imagine that you wish to model the pieces in a game of chess Limited possibilities Limited possibilities Well-defined order Well-defined order Create an enumeration to express those possibilities Create an enumeration to express those possibilities

Conventions Use a capital letter to define the “type” Use a capital letter to define the “type” Use all caps for values Use all caps for values (These are OUR conventions; you must learn the ones used at other locations.) (These are OUR conventions; you must learn the ones used at other locations.) Declare with the enum keyword Declare with the enum keyword

Possibilities Always an integer behind the scenes Always an integer behind the scenes By default start with 0 By default start with 0 By default each value increases by one unit By default each value increases by one unit Possible to do other things Possible to do other things Book shows an example of using char values to define values Book shows an example of using char values to define values

Our Example Use the values assigned by some experts to chess pieces Use the values assigned by some experts to chess pieces pawn = 1 pawn = 1 knight = 3 knight = 3 bishop = 4 (NOTE) bishop = 4 (NOTE) rook = 5 rook = 5 queen = 9 queen = 9 king = 100 (NOTE) king = 100 (NOTE)

Worth Noting The automatic conversion to int can cause us to use values that are not actually included The automatic conversion to int can cause us to use values that are not actually included In many cases it is good to encapsulate knowledge of an enum within a group of functions In many cases it is good to encapsulate knowledge of an enum within a group of functions To do this an enum is often declared as being global to a file of functions To do this an enum is often declared as being global to a file of functions

Topic Structures

Data Associations Programs are supposed to model the real world Programs are supposed to model the real world Many real-world objects have characteristics of different data types Many real-world objects have characteristics of different data types A person: eye color (color), height (double ?), age (int ?), SSN (int ?), home address (strings ?) A person: eye color (color), height (double ?), age (int ?), SSN (int ?), home address (strings ?) Must be able to associate all of these things in our programs Must be able to associate all of these things in our programs

Structures In C++ do this with a “structure” In C++ do this with a “structure” Effectively a new, user-defined data type Effectively a new, user-defined data type Declare with the struct keyword Declare with the struct keyword By convention capitalize the first letter of the new data type By convention capitalize the first letter of the new data type Include elements in curly braces Include elements in curly braces Access elements with the member operator (the dot:.) Access elements with the member operator (the dot:.)

Example Write a program which will test structures using a type called “Pie” Write a program which will test structures using a type called “Pie” Include the following elements: Include the following elements: flavor (string) flavor (string) price (double) price (double) hour baked (int) hour baked (int)

Example (cont.) Assume that pies more than 8 hours old are thrown out Assume that pies more than 8 hours old are thrown out Write a function which will determine the age of a Pie Write a function which will determine the age of a Pie one argument will be the Pie one argument will be the Pie the other will be the current hour the other will be the current hour NOTE: negative numbers indicate an overnight age NOTE: negative numbers indicate an overnight age Write a Boolean function which will say whether a Pie should be kept Write a Boolean function which will say whether a Pie should be kept

In This Lesson We Have: Learned how to create enumerated data types Learned how to create enumerated data types Learned to associate different types of data with one another Learned to associate different types of data with one another