Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 15: User-Defined Types: enum and struct."— Presentation transcript:

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

2 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

3 Topic Enumerations

4 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

5 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

6 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

7 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

8 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)

9 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

10 Topic Structures

11 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

12 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:.)

13 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)

14 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

15 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


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

Similar presentations


Ads by Google