12
CSci 162 Lecture 2 Martin van Bommel

CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Embed Size (px)

DESCRIPTION

Enumeration via constants One method to use enumerated type #defineSunday0 #defineMonday1 #defineTuesday2 #defineWednesday3 #defineThursday4 #defineFriday5 #defineSaturday6 Then can use if (day == Sunday) cout

Citation preview

Page 1: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

CSci 162

Lecture 2

Martin van Bommel

Page 2: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Enumeration

• Enumeration– process of listing all of the elements in the

domain of a type• Enumerated type

– type defined via enumeration• Representation of enumerated type

– give each element a number e.g. 0, 1, 2, 3, ...

Page 3: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Enumeration via constants• One method to use enumerated type#define Sunday 0#define Monday 1#define Tuesday 2#define Wednesday 3#define Thursday 4#define Friday 5#define Saturday 6

• Then can useif (day == Sunday) cout << ”Rest today\n”;

Page 4: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Defining Enumerated Types

• Better to use explicit type definition• Why?

– Compiler chooses integer codes– Programs easier to read with separate, meaningful

type name– Easier to debug the program

Page 5: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Enumerated Type Definitionenum typename { list of elements};

• Exampleenum weekdayT { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

Page 6: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Enumerated types• Values corresponding to elements of types are by default assigned

sequentially from zero• Can change this behavior:enum yearT {Freshman = 1, Sophmore = 2,Junior = 3, Senior = 4 };

• Values assigned sequentially from previousenum yearT{Freshman=1,Sophmore,Junior,Senior

};

Page 7: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Operations of Enumerated Types

• Values of enumerated type is implicitly converted to integer

• Integer not implicitly converted to enumweekdayT day = Monday;int d;d = (day + 1) % 7;day = (weekdayT) d;day = (weekdayT) ( (day + 1) % 7 );

Page 8: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Displaying value of type

• Use function to display value of typevoid PrintDay(weekdayT day){switch(day) { case Sunday: cout << ”Sunday”;break;

case Monday: cout << ”Monday”;break;etc.}

}

Page 9: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Another Way to Display Value

void PrintDay(weekdayT day){ string name[] = {”Sunday”,”Monday”,…}; cout << name[day];}

Page 10: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Characters

• Characters are internally defined almost like an enumerated type

enum char {…,’0’,’1’,…,’9’,’:’,’;’,…,’@’,’A’,’B’,

…,’Z’,’[’,…,’`’,’a’,…,’z’,…};

Page 11: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Boolean Values

• The bool type is an enumerated type

enum bool {false, true};

• Functions with return type “bool” can return “false” or “true” and still be used in “if”

Page 12: CSci 162 Lecture 2 Martin van Bommel. Enumeration –process of listing all of the elements in the domain of a type Enumerated type –type defined via enumeration

Array Indexed by enum• Since values of enumerated type are

implicitly converted to integer, can use as indices to arrayint sales[7], i, total = 0;

sales[Monday] = 10000;

for (i=Sunday; i<=Saturday; i++) {total += sales[i];}