Enumerated Types Mr. Dave Clausen La Cañada High School.

Slides:



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

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
C++ Basics Variables, Identifiers, Assignments, Input/Output.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
True or false A variable of type char can hold the value 301. ( F )
Structure of a C program
Chapter 8 Arrays and Strings
Basic Elements of C++ Chapter 2.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Writing Your First Programs Chapter 2 Mr. Dave Clausen La Cañada High School.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
CPS120: Introduction to Computer Science
C Arrays Systems Programming. Systems Programming: Arrays 22 ArraysArrays  Arrays  Defining and Initializing Arrays  Array Example  Subscript Out-of-Range.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.Semantic © In this session we will.
Simple Data Types Built-In and User Defined Chapter 10.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Arrays.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Module 1: Array ITEI222 - Advance Programming Language.
Programming Fundamentals Enumerations and Functions.
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.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
C++ Lesson 1.
Introduction to ‘c’ language
LESSON 06.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Objectives In this chapter, you will:
Enumeration Type Data type: a set of values with a set of operations on them Enumeration type: a simple data type created by the programmer To define an.
Enumerated Data Types Data type created by programmer
Programming Fundamentals
Basic Elements of C++.
Enumeration Types and typedef
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Instructor: Ioannis A. Vetsikas
Basic Elements of C++ Chapter 2.
Data Types Chapter 8.
2.1 Parts of a C++ Program.
We’re slowly working our way towards classes and objects
Topics 4.1 Relational Operators 4.2 The if Statement
Chapter 2: Introduction to C++.
Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Structures Chapter 4.
Presentation transcript:

Enumerated Types Mr. Dave Clausen La Cañada High School

Mr. Dave Clausen2 Enumerated Types Enumerated TypesEnumerated Types –This is a new simple type that is defined by listing (enumerating) the literal values to be used in this type. The literal values must be identifiers, not numbers.The literal values must be identifiers, not numbers. They are separated in the list from each other with commas.They are separated in the list from each other with commas. The list is enclosed in braces, a.k.a. curly brackets { }The list is enclosed in braces, a.k.a. curly brackets { } Each identifier corresponds to an integer value that represents its position in the list (starting with zero)Each identifier corresponds to an integer value that represents its position in the list (starting with zero) Each identifier is really then a symbolic constant, and therefore, our “style” would capitalize each identifier.Each identifier is really then a symbolic constant, and therefore, our “style” would capitalize each identifier.

Mr. Dave Clausen3 Different from typedef The typedef statement really creates a “synonym” for an existing typeThe typedef statement really creates a “synonym” for an existing type Enumeration creates a new type distinct from any existing type.Enumeration creates a new type distinct from any existing type. Like typedef, the enum declaration must occur before function declarations.Like typedef, the enum declaration must occur before function declarations.

Mr. Dave Clausen4 Program Order with enum CommentsComments Preprocessor Directives (# include…)Preprocessor Directives (# include…) using namespace std;using namespace std; Constant DeclarationsConstant Declarations enum, struct, class (we won’t cover struct or class)enum, struct, class (we won’t cover struct or class) typedef (we won’t cover typedef )typedef (we won’t cover typedef ) function declarations (with their comments before the declaration)function declarations (with their comments before the declaration) int main ( )int main ( )

Mr. Dave Clausen5 enum Definitions enum DayType {SUN, MON, TUE, WED, THUR, FRI, SAT}; enum SuitType {DIAMONDS, CLUBS, HEARTS, SPADES}; enum PetType {CAT, DOG, FISH, RABBIT, TURTLE}; Once the enumerated type is defined, you can create variables of these types in the int main functionOnce the enumerated type is defined, you can create variables of these types in the int main function –DayType day; –SuitType suit; –PetType pet;

Mr. Dave Clausen6 enum Definitions: What you can’t do: enum StarchType {CORN, RICE, POTATO, BEAN}; enum GrainType {WHEAT, CORN, RYE, BARLEY}; You can’t have two of the same identifier in two different enumerated types in the same program. The scope of each enumerated type must be unique. CORN cannot appear in two lists.You can’t have two of the same identifier in two different enumerated types in the same program. The scope of each enumerated type must be unique. CORN cannot appear in two lists. enum VowelType {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; enum FinishType {1st, 2nd, 3rd}; These are both illegal because the items are not valid identifiers.These are both illegal because the items are not valid identifiers. –VowelType is using characters (char) You can’t use predefined data typesYou can’t use predefined data types –FinishType identifiers start with a number

Mr. Dave Clausen7 Order in Enumerated Types By default, the identifiers specified in an enum declaration are given integer values since enums are ordinal types. (Similar to the way that characters are represented by integers using the ASCII code.)By default, the identifiers specified in an enum declaration are given integer values since enums are ordinal types. (Similar to the way that characters are represented by integers using the ASCII code.) –The first identifier in your declaration is given the value of zero. The identifier you listed second is given the value of one, etc.. –Though seldom necessary, you could assign different values to the identifiers in your list when you define them, for example: enum SummerMonthType {JUNE=6, JULY=7, AUGUST=8};

Mr. Dave Clausen8 Operations on Enumerated Types Operations that don’t work:Operations that don’t work: –You cannot input or output enumerated types directly using cin or cout. Given the following: enum WeekDayType {MON, TUE, WED, THUR, FRI}; enum WeekEndDayType {SAT, SUN}; WeekDayType day; day = WED; day = WED; cout<< day; cout<< day; The output for this would be 2 not WED, since only the integer value that represents the position of WED in the list would be assigned to the variable day.

Mr. Dave Clausen9 Output of Enumerated Types Switch statements are usually used to output enum identifiers.Switch statements are usually used to output enum identifiers. void Display_Week_Day (WeekDayType week_day) { switch(week_day) { switch(week_day) { case MON : cout<< “Monday”<<endl; case MON : cout<< “Monday”<<endl; break; break; case TUE : cout<< “Tuesday”<<endl; break; break; case WED : cout<< “Wednesday”<<endl; break; break; case THU : cout<< “Thursday”<<endl; break; break; case FRI : cout<< “Friday”<<endl; break; break;}}

Mr. Dave Clausen10 Input of Enumerated Types Switch statements are usually used to input enum identifiers.Switch statements are usually used to input enum identifiers. void Enter_Week_Day (char week_day_letter, WeekDayType &week_day) { switch(week_day_letter) { switch(week_day_letter) { case ‘M’ : week_day = MON; case ‘M’ : week_day = MON; break; break; case ‘T’ : week_day = TUE; break; break; case ‘W’ : week_day = WED; break; break; case ‘R’ : week_day = THU; break; break; case ‘F’ : week_day = FRI; break; break;}}

Mr. Dave Clausen11 An Operation That Works with Enumerated types: Comparison ComparisonComparison –When you compare two values of enumerated types, the ordering is determined by the order in which the enumerators were placed in their declaration. e.g. enum DayType {SUN, MON, TUE, WED, THUR, FRI, SAT}; //if the variable day contained the value SUN, then the following comparison would be TRUE: day < MON day < MON

Mr. Dave Clausen12 Incrementing Enumerated Types To increment an enumerated type, you must use explicit type conversion (type casting).To increment an enumerated type, you must use explicit type conversion (type casting). –The following don’t use a explicit type conversion and therefore, are invalid: –day = day + 1; //INVALID –day++;//INVALID –for(day=SUN; day <=SAT; day++)//INVALID –The correct way to increment enumerated types is to use explicit type conversion: day = DayType(day + 1);day = DayType(day + 1); for(day = SUN; day <= SAT; day = DayType(day + 1))for(day = SUN; day <= SAT; day = DayType(day + 1))

Mr. Dave Clausen13 Functions that Return an Enumerated Data Type Functions can return any valid data type, simple or user defined, except that of an array.Functions can return any valid data type, simple or user defined, except that of an array. Therefore, a function can return a data type that is enumerated.Therefore, a function can return a data type that is enumerated. We could have written our Enter_Day function with a return type, rather than a void function. Here’s the revised declaration:We could have written our Enter_Day function with a return type, rather than a void function. Here’s the revised declaration: WeekDayType Enter_Week_Day (char week_day_letter);

Mr. Dave Clausen14 Sample Program Using enum BirthdayDev.cpp