220 likes | 386 Vues
Learn about enumeration data types in C++, including declaration, assignment, operations, looping, and anonymous types. Explore the typedef statement, namespaces, and the limitations of string types.
E N D
Data Types Chapter 8
Chapter Topics • Enumeration Data Types • Declaration • Assignment • Operations • Looping with Enumeration Types • Anonymous Data Types • The typedef statement • Namespaces • The string type
Enumeration Data Types • A data type is • A set of values together with • A set of operations on those values. • In order to define a new simple data type, called enumerationtype, we need: • A name for the data type. • A set of values for the data type. • A set of operations on the values.
Enumeration Data Types • C++ allows the user to define a new simple data type by specifying: • Its name and the values • But not the operations. • The values that we specify for the data type must be legal identifiers • The syntax for declaring an enumeration type is: enum typeName{value1, value2, ...};
Declaration of Enumerated Types • Consider the colors of the rainbow as an enumerated type:enum rainbowColors = { red, orange, yellow, green, blue, indigo, violate } • The identifiers between { } are called enumerators • The order of the declaration is significantred < orange < yellow …
Declaration of Enumerated Types • Why are the following illegal declarations? enum grades{'A', 'B', 'C', 'D', 'F'}; enum places{1st, 2nd, 3rd, 4th}; They do not have legal identifiers in the list • What could you do to make them legal? enum grades{A, B, C, D, F}; enum places{first, second, third, fourth};
Declaration of Enumerated Types • As with the declaration of any object • Specify the type name • Followed by the objects of that type Given: enum daysOfWeek { Sun, Mon, Tue,Wed, Thu, Fri, Sat } Then we declare: daysOfWeek Today, payDay, dayOff;
Assignment with Enumerated Types • Once an enumerated variable has been declared • It may be assigned an enumerated value • Assignment statement works as expected payDay = Fri; // note no quotes // Fri is a value, a constant • Enumerated variables may receive only values of that enumerated type
Operations on Enumerated Type Objects • Incrementing variables of an enumerated type • Do NOT use workaday += 1; NOR today++; • Instead, use explicit type conversiontoday = daysOfWeek (today + 1);
Operations on Enumerated Type Objects • Comparison • normal, OK • in order of the enumeration definition • I/O • generally not possible to do directly • can be sort of done, indirectly • Used primarily for program control, branching, looping • Possible to have functions return an enumerated type
Looping with Enumeration Types This works because the values are represented internally as integers • Use an enumerated type variable as the loop control variable of a for loop for (day = Mon; day < Sat; day = static_cast<daysOfWeek>(day + 1)) { . . . }
Functions with Enumerated Types • Enumeration type can be passed as parameters to functions either by value or by reference. • A function can return a value of the enumeration type. daysOfWeek nextDay (daysOfWeek d){ return (daysOfWeek) ((d + 1)%7);}
Anonymous Data Types • Named Type • user defined type • declaration includes typedef • As with daysOfWeek or Boolean • Anonymous Type • does not have an associated type enum (MILD, MEDIUM, HOT) salsa_sizzle; • variable declared withouttypedef
Anonymous Data Types Disadvantages • Cannot pass an anonymous type as a parameter to a function. • A function cannot return a value of an anonymous type. • Problems when: enum {English, French, Spanish, German, Russian} languages; enum {English, French, Spanish, German, Russian} foreignLanguages; languages = foreignLanguages; //illegal Same values used but variables treated as non compatible types
The typedef statement • Syntax: typedef existing_type_name new_type_name; • Example:typedef int Boolean; • Does not really create a new type • is a valuable tool for writing self-documenting programs
Namespaces • Recall use of using namespace std; • Namespace is another word for scope • In C++ it is a mechanism • programmer creates a "named scope"namespace std{ int abs ( int ); . . .}
Namespaces • Identifiers within a namespace can be used outside the body of the declaration only if • use scope resolution operatorx = std::abs(y); • a using declarationusing std::abs;z = abs(q); • a using directiveusing namespace std;p = abs(t); Note the distinction between declaration and directive
Namespaces • We usually place the using directive in global scope • All blocks { . . . } then have identifiers available from the std namespace
The string type • We have used arrays of char to hold "string" information char name[30];cin >> name; • There are some problems with doing this • There is no assignment statement • Must use strcpy (name, "Clyde"); • Cannot do comparisons with < or == or > • Must use if (strcmp (s1, s2) == 0) … • For all these must use #include <string.h>
The string type • C++ has a string type which bypasses all the problems we've encountered • Gain these capabilities by#include <string> // note no .h • Now we can use statements as shown: string name = "Clyde"; if (title1 < title2) … str1 = str1 + "Day"; // assignment and concatenation
The string type • Some functions are available string name, title;name = "Alexander";cout << name.length()<<endl;cout << name.find('x') <<endl;cout << name.substr(1,3) <<endl;title = "Big Cheese";title.swap(name);cout << name<<endl; Guess what will be the output of these lines of code