enumerated datatypes. user defined data types data type defined by programmer allows use of more...

10
ENUMERATED DATATYPES

Upload: damian-wilson

Post on 13-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATATYPES

Page 2: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

USER DEFINED DATA TYPES

Data Type Defined By Programmer Allows Use Of More Complex Data Typically Defined Globally So Variables Of The

Type Can Be Declared Wherever Needed Types Include:

enumerated arrays structures array of structures linked list

Page 3: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

User Defined Type Used To Increase Readability Of Programs

Assigns Meaningful Name To Value Programmer Defines Names Of Values Internal Data Type That Cannot Be Read Or

Written

Page 4: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

Requires List Of Values (Enumerated Constants) List Is Identified With A New Data Type Values Are Not char, string Or Number

No ‘ ‘ or “ “ Around Constants

Constants Must Be Valid Identifier Names Constants Cannot Be Used In Another

Enumerated Data Type In Same Program Proper Syntax:

enum colors {BLACK, RED, GREEN, BLUE, BROWN};

Page 5: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

Declaring Data Type Does Not Allocate Memory Variables Of Type Needs To Be Declared

colors shirtColor, pantColor

This Creates Two Variables That Can Store Enumerated Constant

shirtColor = RED;

pantColor = BLUE;

Page 6: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

The Enumerated Constants Are An Ordered List Of Values (ordinal type)

The First Constant Has The Ordinal Value Zero Each Successive Constant Is One Greater Internally The Data Type Stored When Using The

Enumerated Constant Is Integer

cout << pantColor << ‘ ‘ << shirtColor << endl;

displays: 3 1

Page 7: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

Constants Are Ordinal So Relational Operators Can Be Used

if (shirtColor > pantColor)

cout << “You May Want To Change Your Shirt” << endl;

Arithmetic Operators Cannot Be Used Directly

shirtColor = pantColor + 1; // THIS IS ILLEGAL

However Use Of Static Cast Allows Addition / Subtraction

shirtColor = static_cast<colors>(pantColor + 1) // IS LEGAL

Page 8: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

WARNING, WARNING, WARNING….

There Is No Predecessor Before The First Constant And No Successor After The Last Constant

Trying To Subtract One From The First Or Add One To The Last Causes An Undefined Value

Page 9: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

Two Major Drawbacks: Cannot Be Input Directly From Keyboard Cannot Be Output Directly To The Screen (except for

ordinal value)

Solving Problem Requires: Creating An Input Function That Accepts Allowed

Input And Converts To Enumerated Value Creating An Output Function That Converts

Enumerated Value To A Value That Can Be Displayed

Page 10: ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables

ENUMERATED DATA TYPE

In The End: Enumerated Data Types Provide For More Readable

Code But Often Require Extra Coding To Make Them

Useable