ENUMERATED DATATYPES 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 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 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}; 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; 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 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 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 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 ENUMERATED DATA TYPE In The End: Enumerated Data Types Provide For More Readable Code But Often Require Extra Coding To Make Them Useable