15
0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int, float, char What about problems that don’t eg representing a pack of cards— suits (hearts, clubs, diamonds, spades), ranks (2-10, Jack - Ace) representing grading schemes A++, A+, A, A-, B+ etc… Could simulate by manipulating strings etc unwieldy, error prone We want to define our own types— called user-defined types or enumerated types Advantages allow us to specify a restricted range of values for our type—type safety increase readability of our programs

0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

Embed Size (px)

Citation preview

Page 1: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 1

Enumerated types

We already know some data types• int, float, char• good for problems that involve int, float,

char

What about problems that don’t• eg representing a pack of cards—suits

(hearts, clubs, diamonds, spades), ranks (2-10, Jack - Ace)

• representing grading schemes A++, A+, A, A-, B+ etc…

Could simulate by manipulating strings etc

• unwieldy, error prone

We want to define our own types—called• user-defined types or• enumerated types

Advantages• allow us to specify a restricted range of

values for our type—type safety• increase readability of our programs

Page 2: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 2

Enumerated types

Declare

Use

Effect• the compiler will make sure that only TRUE

or FALSE are assigned to a variable of type boolean

• you’ve reduced the chance of errors in your program

• you’ve made your program more readable…..

enum boolean {FALSE, TRUE};reservedword

identifier foryour type

list of validvalues

boolean ok = FALSE;boolean checkval;checkval = TRUE;

Page 3: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 3

Enumerated types

enum suit {Clubs, Diamonds, Hearts, Spades};enum rank {Two, Three, Four, Five, Six,

Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};

suit s1, s2, s3;rank rank1;

s1 = Clubs;s2 = Hearts;rank1 = Six;

if (rank1 < Ten) { cout << “Loser!” << endl; }if (s2 > s1) { cout << “Winner!” << endl;}

Why can we make comparisons like this?• because the values are enumerated• values begin at 0

enum suit {Clubs0, Diamonds1, Hearts2, Spades3};enum rank {Two0, Three1, Four2, Five3, Six4,

Seven5, Eight6, Nine7, Ten8, Jack9, Queen10, King11, Ace12};

Page 4: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 4

Switch and enums

Switch is really useful with enum:

Considerenum day {Sunday, Monday, Tuesday, …};

day today;

// read in today’s value somewhere

switch (today) {

case Saturday:

pay_factor = 1.5;

break;

case Sunday:

pay_factor = 2.0;

break;

case default:

pay_factor = 1.0;

}

Page 5: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 5

Writing out enum values

Because you create the enumerated values, you have to deal with writing them out yourself – so not like int, compiler knows nothing about what they represent:

void print_day(day today) {

switch (today) {

case Monday:

cout << “Monday”;

break;

case Tuesday:

cout << “Tuesday”;

break;

case default:

cout << “ERROR”;

}

}

Page 6: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 6

Records (a.k.a. Structures)

We already know some data types• int, float, char, enumerated• good for problems that involve a single

type per object to be represented

What about a student?• name (string)• age (integer)• gender (enumerated)• id number (integer)• grade point average (float)

One solution

Seems OK….. ??

char name[40];int age;gender_type gender;int id;float gpa;

Page 7: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 7

Records (a.k.a. Structures)

How appropriate is that solution for the 104 class?

We would need to• repeat all of the declarations for every

student in the class• use a different variable name for each

student• add new variables if we have a new

attribute of a student to store eg address

char student1_name[40];int student1_age;gender_type student1_gender;int student1_id;float student1_gpa;char student2_name[40];int student2_age;gender_type student2_gender;int student2_id;float student2_gpa;..char student270_name[40;

Page 8: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 8

Declaring Structures

Ideally we would like to describe a student by grouping together multiple variables that may be of different types

Structures allow us to do this

generally

enum gender_type {Male, Female};

struct {char name[40];int age;gender_type gender;int id;float gpa;

} student1, student2, student3;

struct {<type> <member identifier>;..<type> <member identifier>;

} <list of variable names>;

Page 9: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 9

Accessing structure members

The structure variables (student1, student2, student3) are made up of multiple members or fields

Members are accessed using a selector which indicates the required structure variable and member

Combine the variable and member with a full stop eg

enum gender_type {Male, Female};

struct {char name[40];int age;gender_type gender;int id;float gpa;

} student1, student2, student3;

student1.gpa = 5.6;student2.id = 965051;

cout << student1.gpa << endl;

Page 10: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 10

Visualising structures

A structure variable refers to a group of data items

A member name refers to a data item contained with a structure variable

student1

name

gender

age

id

gpa

Page 11: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 11

Defining structured types

The current approach combines the definition of the structure members and the structure variables

Not very flexible!

We can define structured types, and then use them to define variables

enum gender_type {Male, Female};

struct studentType {char name[40];int age;gender_type gender;int id;float gpa;

};

type name

no variable identifies

Page 12: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 12

Defining structured types

Now we can declare multiple variables of this type

enum gender_type {Male, Female};

struct studentType {char name[40];int age;gender_type gender;int id;float gpa;

};

studentType student1;studentType student2;studentType student3, student4, student5;

Page 13: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 13

Defining structured types

Name of members will NOT conflict with other variables of the same name

is OK because the members require a selector that includes the variable name

BUT be careful - can be confusing!

enum gender_type {Male, Female};

struct studentType {char name[40];int age;gender_type gender;int id;float gpa;

};

studentType student1;studentType student2;studentType student3, student4, student5;int id;float gpa;

Page 14: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 14

Initialising structure variables

Structure variable members can be initialised in a single statement

Values in initialisations MUST appear in same order as members of the structure

Accessing members of a structure

Can only assign between complete structs of the same type

CANNOT test equality or inequality with == or != MUST test each member in turn

studentType student_1 = {“steve”, 19, Male, 991654, 8.5};studentType student_2 = {“sally”, 40, Female, 997652, 9.5},

student_3, student_4;

cout << “Name : “ << student_1.name << endl;cout << “Id : “ << student_1.id << endl;cout << “Age : “ << student_1.age << endl;

student_3.gpa = 7.3;student_4 = student_2;

Page 15: 0657.104 Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,

0657.104 Introduction to Computer Science 2 Slide 15

There's still a problem

Although we have now grouped all the data items for a student within a structure we still need multiple variables to represent multiple students

How can we group students together to represent a class?

What data structure do you know about that groups data items of the same type?

HINT:

studentType students[200];

students[0].name = “john”;

students[1].name = “sally”;