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

Similar documents
Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Data Types. Chapter 8

Lecture 3. The syntax for accessing a struct member is

PASCAL. PASCAL, like BASIC, is a computer language. However, PASCAL, unlike BASIC, is a Blocked Structured Language (BASIC is known as unstructured).

Chapter 11: Structured Data

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

12 CREATING NEW TYPES

car object properties data red travels at 50 mph actions functions containing programming statements Program Program variables functions

Chapter 11: Structured Data

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

Chapter 8 - Notes User-Defined Simple Data Types, Namespaces, and the string Type

CSE 230 Intermediate Programming in C and C++

AIMMS Function Reference - Date Time Related Identifiers

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types

Arrays III and Enumerated Types

Chapter Two MULTIPLE CHOICE

CSCI 1061U Programming Workshop 2. C++ Basics

Input And Output of C++

Introduction to Computers and Programming

LECTURE 11 STRUCTURED DATA

More non-primitive types Lesson 06

BLM2031 Structured Programming. Zeyneb KURT

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

COMP322 - Introduction to C++

C++/Java. C++: Hello World. Java : Hello World. Expression Statement. Compound Statement. Declaration Statement

Objectives. In this chapter, you will:

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

CS349/SE382 A1 C Programming Tutorial

Daily Math Week 8 ( ) Mon. October 7, 2013 Tues. October 8, 2013 Wed. October 9, 2013 Thurs. October 10, 2013 Fri.

Basic Elements of C. Staff Incharge: S.Sasirekha

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

CS2141 Software Development using C/C++ C++ Basics

CS 485 Advanced Object Oriented Design. Enum. Spring 2017

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

CSI33 Data Structures

Variables. Data Types.

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Daily Math Week 10 ( ) Mon. October 21, 2013 Tues. October 22, 2013 Wed. October 23, 2013 Thurs. October 24, 2013 Fri.

Chapter. Solving Problems that Require Decisions. Objectives: At the end of the chapter students should be able to:

Francesco Nidito. Programmazione Avanzata AA 2007/08

Bitwise Operators. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

Computer Science 5C Chapter 7--Enumeration Types and Typedef

Motivation. Each distinct type needs its own I/O library INTEGER FLOAT CHARACTER BOOLEAN. Using pure numbers, we can write nonsense:

C C C C++ 2 ( ) C C++ 4 C C

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013

Objectives. Describe ways to create constants const readonly enum

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

C++ PROGRAMMING BASICS

DigiPen Institute of Technology

Lab Instructor : Jean Lai

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Pointers and Strings Chapters 10, Pointers and Arrays (10.3) 3.2 Pointers and Arrays (10.3) An array of ints can be declared as

Character Functions & Manipulators Arrays in C++ CS 16: Solving Problems with Computers I Lecture #10

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Decisions. Arizona State University 1

Industrial Programming

Chapter 2 Basic Elements of C++

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

BITG 1233: Introduction to C++

Homework #3 CS2255 Fall 2012

Lecture 12: Data Types (and Some Leftover ML)

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Introduction. A4 Elem of Style. Introduction

Tokens, Expressions and Control Structures

C++ Programming: From Problem Analysis to Program Design, Third Edition

Introduction to C Final Review Chapters 1-6 & 13

C++ Support Classes (Data and Variables)

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

UEE1302 (1102) F10: Introduction to Computers and Programming

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Programming in C. What is C?... What is C?

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

SUN Sun Certified Associate for the Java Platform.

Programming with C++ as a Second Language

CHAPTER 3 BASIC INSTRUCTION OF C++

CSCE 314 Programming Languages

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

The C++ Language. Arizona State University 1

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

Structures and Pointers

Chapter 6 part 1. Data Types. (updated based on 11th edition) ISBN

Data Types. 9. Types. a collection of values and the definition of one or more operations that can be performed on those values

typedef int Array[10]; String name; Array ages;

Transcription:

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

Enumerated Types Enumerated 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. They are separated in the list from each other with commas. 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 is really then a symbolic constant, and therefore, our style would capitalize each identifier. Mr. Dave Clausen 2

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

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

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 function DayType day; SuitType suit; PetType pet; Mr. Dave Clausen 5

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. enum VowelType { A, E, I, O, U }; enum FinishType {1st, 2nd, 3rd}; These are both illegal because the items are not valid identifiers. VowelType is using characters (char) You can t use predefined data types FinishType identifiers start with a number Mr. Dave Clausen 6

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.) 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 Clausen 7

Operations on Enumerated Types 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; 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 Clausen 8

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

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

An Operation That Works with Enumerated types: Comparison Comparison 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 Mr. Dave Clausen 11

Incrementing Enumerated Types 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); for(day = SUN; day <= SAT; day = DayType(day + 1)) Mr. Dave Clausen 12

Functions that Return an Enumerated Data Type 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. 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 Clausen 13

Sample Program Using enum BirthdayDev.cpp Mr. Dave Clausen 14