190 likes | 314 Views
Built- in Simple Types Enumerated Data Type Switch Statement. Built- in Simple Types - We have seen a number of simple built-in types - int, char, float, bool - by a simple type we mean not an array or … - We have also seen (vaguely) how some of these types are represented
E N D
Built- in Simple Types Enumerated Data Type Switch Statement
Built- in Simple Types - We have seen a number of simple built-in types - int, char, float, bool - by a simple type we mean not an array or … - We have also seen (vaguely) how some of these types are represented - An important attribute of a type is its size - how many bytes is required to store an “int”? - this is not an easy question to answer
Built- In Simple Types - C++ provides the sizeof operator to find the size of a particular type - For example: cout << sizeof(int) << “ ” << sizeof( char) << “ ” << sizeof( float) << endl; - This might print out 4 1 4 - But then again, it might not… - the sizes of the simple data types is compiler or architecture (machine-type) specific
Built- In Simple Types - C++ does provide a number of different kinds of ints - short (or “short int”) - long ( or “long int”) - C++ does guarantee that: 1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) - In UNIX C++, sizeof(short)=2, sizeof(int)= 4, sizeof(long)=4 - For int types the maximum and the minimum size are - For Max = 2 (number of bits-1) -1 - For Min = 2 (number of bits-1) < See Example 1 - part 1>
Built- In Simple Types - Can also specify that an integer type is unsigned: represents a positive integer unsigned int x; x = 0; x = -2; // This Does not give you the correct answer - an unsigned int can hold values 0.. UINT_ MAX - an unsigned short can hold values 0.. USHRT_ MAX - an unsigned long can hold values 0.. ULONG_ MAX < See Example 1 - part 2>
Built-In Simple Types - Why have unsigned numbers? - tell the programmer and code-reader that a number is positive - tells the compiler that a number is unsigned
Built- In Simple Types - There are also different kinds of floating point numbers - float - standard floating point numbers - double - bigger floating point numbers - long double - bigger still floating point numbers - As with int’s, the sizes of these types is not defined by the language but by the compiler/system - Guaranteed: sizeof(float) <= sizeof(double) <= sizeof(long double)
Built- In Simple Types - The standard head file float.h defines the limits on your system #include <cfloat> cout << FLT_ MIN << FLT_ MAX // smallest and largest float cout << DBL_ MIN << DBL_ MAX // smallest and largest double cout << LDBL_ MIN << LDBL_ MAX // smallest and largest // long double < See Example 1 - part 3>
Typedef The typedef statement is the simplest way to create your own type - essentially just gives a new name to an existing type - this will be more useful with more complex types in the future - Syntax: typedef ExistingTypeName NewTypeName;
Typedef Example: typedef long double bigfloat; bigfloat x, y; - define a new type bigfloat, which is the same as “long double”. - can now declare variables/ parameters/ arrays, etc. of type bigfloat < See Example 1 - part 4>
Enumeration Types - In C++, we can define a new type by listing the possible literal values that make up the possible values for the type - called an Enumeration Type - Example: enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; defines a new type Days which has possible values Sun, Mon, Tue, Wed, Thu, Fri, Sat
Enumeration Types For example: enum Days {Sun, Mon, Tue, Wed, Thu, Fri, Sat}; Days today; today = Fri; if (today == Sat) cout << “It’s saturday!!!\n”; < See Example 1 - part 5&6>
Enumeration Types - Rules and regulations for enumerated types - the possible values for the type (Sun.. Sat in this case) are called enumerators
Enumeration Types - Enumerations behave much as do ints - In the Days example get equivalencies: Sun Mon Tue Wed Thu Fri Sat 0 1 2 3 4 5 6 - enumerators may be compared using all standard comparison operators - the order of enumerators is the order of their corresponding int values - in this case Sun < Mon < Tue < ........
Enumeration Types - Few functions are defined on enumerations - you can assign variables of enumerations - you can compare variables of enumerations - you cannot add, subtract, or increment (++) enumerations - you cannot read an enumeration directly - you can use an enumeration pretty much anywhere you use an int and it will use the corresponding int value - for example, if you print an enum type, it will print the corresponding int
Enumeration Types and Switch Statement - Enumeration types can be used in a switch statement Example of Switch statement: Days today; … switch (today) { case Sun: cout << “Sunday”; break; case Mon: cout << “Monday”; break; case Tue: cout << “Tuesday”; break; case Wed: cout << “Wednesday”; break; case Thu: cout << “Thursday”; break; case Fri: cout << “Friday”; break; case Sat: cout << “Saturday”; break; } < See Example 1 - part 7>
Enumeration Types - The iostream library does not support I/O of enumerated types - To do output, typically do a switch statement much like the previous one - Input is somewhat trickier - one possibility is to use integers to represent days
Enumeration Types - Example: input of enumeration type using int to represent the enumerated type Days. …. int today_int; cout << "Enter today (0= Sun, 1= Mon, ..., 6= Sat): "; cin >> today_int; switch (today_int) { case 0: today= Sun; break; case 1: today= Mon; break; case 2: today= Tue; break; case 3: today= Wed; break; case 4: today= Thu; break; case 5: today= Fri; break; case 6: today= Sat; break; default: cout << "Illegal day code " << endl; break; }; < See Example 1 - part 8>