DOC PREVIEW
Saddleback CS 1B - Enumeration Types typeDef Header Files

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS1B Introduction to Programming in C Enumeration Types typeDef Header Files Topic 5 Chapter 9 in the shrinkwrap Chapter 8 in Malik Saddleback College Spring 2009 J Tateyama CS1C Saddleback College Enumeration Types C allows the user to define a new simple data type by listing the literal values that make up the type This listing is referred to as enumeration which means that each element of the type has a numeric value associated with it to allow for comparison etc Enumeration types are used primarily to add readability to a program Enumeration Type A user defined data type consisting of an ordered set of literal values expressed as identifiers Enumerator One of the ordered values identifiers in the list of an enumeration type The enumerators are stored in memory as integer values just the same way that values of type char are stored as integers Enumerators are essentially named constants of the type so we will use uppercase letters for these identifiers General Form enum Name IDENT IDENT CS1C Saddleback College Enumeration Example Example declaration of data type Day enum Day SUN MON TUE WED THU FRI SAT A numeric value is associated with each member of the type SUN has a value of 0 MON a value of 1 etc This allows for comparisons of the members of the type The identifiers inside the braces must be constants that explicitly name the members of the type The same identifier may not appear in two enumeration lists The identifiers must follow the rules for any other C identifier Declaration of two variables of type Day Day workDay Day funDay CS1C Saddleback College What s wrong enum Days SUN MON TUE WED THU FRI SAT No quotes allowed Note You might come across some C code that does the following enum Days SUN 6 MON 2 TUE 12 WED 1 This is legal but rarely used It would change the enumeration values of the members of the type SUN would come after MON for the purposes of comparison CS1C Saddleback College Assignment Statements Assignments to enumeration types workDay TUE funDay SAT workDay funDay funDay 6 error Type coercion Implicit type coercion is defined from an enumeration type to int But NOT from int to an enumeration type Can you increment and decrement enumeration types No because it would require type coercion But you can use the enum type to control loops just as the type int may be used For example the loop could count from MON to FRI CS1C Saddleback College For Loop Example Enum LightClass GREEN YELLOW RED LightColors trafflt For trafflt GREEN trafflt RED trafflg LightColors trafflt 1 operators are overloaded for for loops but not the operators You must use explicit type casting as in the example above Also it is not possible to perform direct input output on enumeration types CS1C Saddleback College Enumeration Example Declare an enum type called Dogs that lists four breeds a spaniel a golden retriever a poodle and a lab enum Dogs POODLE SPANIAL LAB RETRIEVER Write the prototype for a function called ConvertType that receives a string and returns a member of type Dogs Dogs ConvertType string d Write the prototype for a function called Output Type that receives a member of type Dogs and outputs the breed to the screen void OutputType Dogs d CS1C Saddleback College Enumeration Example int main declare the necessary variables to hold the breed of my dog and your dog and read this information from the keyboard Dogs myDog yourDog string tempDog read the breeds from the keyboard and assign the appropriate type cout My Breed cin tempDog myDog ConvertType tempDog cout Your Breed cin tempDog yourDog ConvertType tempDog output the dog types cout I have a beautiful OutputType myDog and you have a OutputType yourDog CS1C Saddleback College Enumeration Example both have the sameyourDog breed if we myDog cout You are lucky that you have one too else cout I guess a output the type of your dog OutputType yourDog cout is ok too CS1C Saddleback College Function Definitions Write the function definitions for ConvertType and OutputType for the enum Dogs POODLE SPANIEL LAB RETRIEVER used in the previous example Dogs ConvertType string d user enters Poodle Spaniel Lab Retriever so could use first character for switch Dogs tmp Switch toupper d 0 case P tmp POODLE break case S tmp SPANIEL break return tmp CS1C Saddleback College Function Definitions void OutputType Dogs dog Switch dog case POODLE cout POODLE break case SPANIEL cout Spaniel break case LAB cout Lab break case GOLDEN RETRIEVER cout Golden Retriever CS1C Saddleback College Named Types Consider the following enum Dogs POODLE SPANIEL LAB GOLDEN RETRIEVER versus the declaration enum POODLE SPANIEL LAB GOLDEN RETRIEVER myDog yourDog The first declaration created a data type which is named and can be used to declare variables use type as parameter use type as a function return value Named type is a user defined data type that uses an identifier in the declaration that associates a name with the type CS1C Saddleback College Anonymous Type In the second declaration enum POODLE SPANIEL LAB GOLDEN RETRIEVER myDog yourDog Two variables have been declared whose possible values are the literal values in the declaration We cannot use the enum type as a parameter use the enum type as a return type This is an example of an Anonymous type which is a user defined data type that does not have a name associated with it Good programming style dictates the use of named types CS1C Saddleback College typedef Statement The typedef statement is used not to create a new type but instead to give a new name to an existing type Example Prior to the introduction of the bool type we could define our own type to add readability to a program typedef int Boolean const int TRUE 1 const int FALSE 0 Boolean loopFlag a variable that may contain TRUE or FALSE Although loopFlag actually contains an integer value the program is made more clear by using the named constants TRUE and FALSE CS1C Saddleback College Array typedef Example typedef float FloatArrayType 100 anything of type FloatArrayType is defined as a 100 element array of float values FloatArrayType myArray myArray is a variable representing a 100 element array of float values Because the compiler knows the size and type of the array from the definition of the type FloatArrayType we can now use this type in a formal parameter list void LoadArray FloatArrayType anArray The typedef statement allows us to create code that is more self documenting and easier to understand Review typeDef array example CS1C Saddleback


View Full Document

Saddleback CS 1B - Enumeration Types typeDef Header Files

Download Enumeration Types typeDef Header Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Enumeration Types typeDef Header Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Enumeration Types typeDef Header Files and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?