1 / 85

Chapter 10 Simple Data Types: Built-In and User-Defined Dale/Weems/Headington

Chapter 10 Simple Data Types: Built-In and User-Defined Dale/Weems/Headington. Chapter 10 Topics. External and Internal Representations of Data Integral and Floating Point Data Types Using Combined Assignment Operators Prefix and Postfix Forms of Increment and Decrement Operators

kioshi
Download Presentation

Chapter 10 Simple Data Types: Built-In and User-Defined Dale/Weems/Headington

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 10 Simple Data Types: Built-In and User-Defined Dale/Weems/Headington

  2. Chapter 10 Topics • External and Internal Representations of Data • Integral and Floating Point Data Types • Using Combined Assignment Operators • Prefix and Postfix Forms of Increment and Decrement Operators • Using Ternary Operator • Using Type Cast Operator • Using an Enumeration Type • Creating and Including User-Written Header Files

  3. Enumeration Types

  4. floating address float double long double pointer reference C++ Data Types (enum) simple structured integral enum array struct union class char short int long bool

  5. typedefstatement • typedef creates an additional name for an already existing data type • before bool type became part of ISO-ANSI C++, a Boolean type was simulated this way typedef int Boolean; const Boolean true = 1 ; const Boolean false = 0 ; . . . Boolean dataOK ; . . . dataOK = true ;

  6. Enumeration Type • A user-defined data type • whose domain is • an ordered set of literal values • expressed as identifiers • Enumerator • one of the values • in the domain of an enumeration type

  7. Declaring Enumeration Types • Syntax: • enum NewTypeName { List }; • where • NewTypeName is a valid identifier • List is a list of items, each of which is • Identifier OR • Identifier = IntegerConstant

  8. name of new type list of all possible values of this new type Enumeration Types • C++ allows creation of a new simple type by listing (enumerating) all the ordered values in the domain of the type EXAMPLE enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ;

  9. Enumeration Example enum Days {SUN, MON, TUE, WED, THU, FRI, SAT}; The effect of the enum is the same as: typedef int Days; const int SUN = 0; const int MON = 1; const int TUE = 2; const int WED = 3; const int THU = 4; const int FRI = 5; const int SAT = 6;

  10. enum Type Declaration enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; • the enum declaration creates a new programmer-defined type and lists all the possible values of that type--any valid C++ identifiers can be used as values • the listed values are ordered as listed. That is, JAN < FEB < MAR < APR , and so on • you must still declare variables of this type

  11. Declaring enumType Variables enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; MonthType thisMonth; // declares 2 variables MonthType lastMonth; // of type MonthType lastMonth = OCT ; // assigns values thisMonth = NOV ; // to these variables . . . lastMonth = thisMonth ; thisMonth = DEC ;

  12. Storage of enumType Variables stored as 0 stored as 1 stored as 2 stored as 3 etc. enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; stored as 11

  13. Use Type Cast to IncrementenumType Variables enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; MonthType thisMonth; MonthType lastMonth; lastMonth = OCT ; thisMonth = NOV ; lastMonth = thisMonth ; thisMonth = thisMonth++ ; // COMPILE ERROR ! thisMonth = MonthType( thisMonth + 1) ; // uses type cast

  14. Example Enumerations: enum CoolCars {LAMBORGHINI, BMW, PORSCHE, FERRARI, LOTUS}; enum ClassicTV {BEVERLYHILLBILLIES, GILLIGANSISLAND, BATMAN, ILOVELUCY, OUTERLIMITS, TWILIGHTZONE}; enum Department {CSC, MATH, BIOL, CHEM, PHYS, ENGR}; enum Boolean {FALSE, TRUE}; • Data type Boolean has two values: • FALSE (0) and TRUE (1) enum EscapeSequence {NEWLINE = '\n', HORIZTAB = '\t', VERTTAB = '\v', BACKSPACE = '\b'};

  15. Enumeration Errors • enum Vowel {'A', 'E', 'I', 'O', 'U'}; • enum Places {1st, 2nd, 3rd}; • enum Starch {CORN,RICE,POTATO,BEAN}; • enum Grain {WHEAT,CORN,RYE,BARLEY,SORGHUM};

  16. More aboutenum Type Enumeration type can be used in a Switch statement for the switch expression and the case labels. Stream I/O ( using the insertion << and extraction >> operators ) is not defined for enumeration types. Instead, functions can be written for this purpose. Comparison of enum type values is defined using the 6 relational operators ( < , <= , > , >= , == , != ). An enum type can be the return type of a value-returning function in C++. SOME EXAMPLES . . .

  17. MonthType thisMonth; switch ( thisMonth ) // using enum type switch expression { case JAN : case FEB : case MAR : cout << “Winter quarter” ; break ; case APR : case MAY : case JUN : cout << “Spring quarter” ; break ; case JUL : case AUG : case SEP : cout << “Summer quarter” ; break ; case OCT : case NOV : case DEC : cout << “Fall quarter” ; }

  18. Using enum type Control Variable with for Loop enum MonthType { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } ; void WriteOutName ( /* in */ MonthType ) ; // prototype . . . MonthType month ; for (month = JAN ; month <= DEC ; month = MonthType (month + 1 ) ) {// requires use of type cast to increment WriteOutName ( month ) ; // function call to perform output . . . }

  19. void WriteOutName ( /* in */ MonthType month ) // Prints out calendar name corresponding to month // Precondition: month is assigned // Postcondition: calendar name for month has been written out {switch ( month ) { case JAN : cout << “ January ” ; break ; case FEB : cout << “ February ” ; break ; case MAR : cout << “ March ” ; break ; case APR : cout << “ April ” ; break ; case MAY : cout << “ May ” ; break ; case JUN : cout << “ June ” ; break ; case JUL : cout << “ July ” ; break ; case AUG : cout << “ August ” ; break ; case SEP : cout << “ September ” ; break ; case OCT : cout << “ October ” ; break ; case NOV : cout << “ November ” ; break ; case DEC : cout << “ December ” ; break ; } }

  20. Function with enum type Return Value enum SchoolType { PRE_SCHOOL, ELEM_SCHOOL, MIDDLE_SCHOOL, HIGH_SCHOOL, COLLEGE } ; . . . SchoolType GetSchoolData ( void ) // Obtains information from keyboard to determine school level // Postcondition: Function value == personal school level { SchoolType schoolLevel ; int age ; int lastGrade ; cout << “Enter age : “ ; // prompt for information cin >> age ;

  21. if ( age < 6 ) schoolLevel = PRE_SCHOOL ; else { cout << “Enter last grade completed in school : “ ; cin >> lastGrade; if ( lastGrade < 5 ) schoolLevel = ELEM_SCHOOL ; else if ( lastGrade < 8 ) schoolLevel = MIDDLE_SCHOOL ; else if ( lastGrade < 12 ) schoolLevel = HIGH_SCHOOL ; else schoolLevel = COLLEGE ; } return schoolLevel ;// return enum type value }

  22. Data Types

  23. Data Type • A specific set of data values • for int, whole numbers • from INT_MIN to INT_MAX • With a set of operations on those values • for int: +,-,*,/,%,++,-- • relational and logical operators

  24. Simple Data Type • A data type • in which each value • is atomic (indivisible) • 'Q' is atomic • no parts to be accessed individually • "Good Morning" is not atomic • it is composed of several chars

  25. simple types integral floating char short int long bool enum float double long double unsigned C++ Simple Data Types

  26. Data Type Requirements

  27. By definition, the size of a C++ char value is always 1 byte. exactly one byte of memory space Sizes of other data type values in C++ are machine-dependent. ‘A’

  28. Using one byte ( = 8 bits ), HOW MANY DIFFERENT NUMBERS CAN BE REPRESENTED USING 0’s and 1’s? Each bit can hold either a 0 or a 1. So there are just two choices for each bit, and there are 8 bits. 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256 0 1 1 0 0 0 1 1

  29. 0 1 0 0 1 0 1 0 0 1 1 0 0 0 1 1 Similarly, using two bytes ( = 16 bits), 216 = 65,536 DIFFERENT NUMBERS CAN BE REPRESENTED. If we wish to have only one number representing the integer zero, and half of the remaining numbers positive, and half negative, we can obtain the 65,536 numbers in the range below : -32,768 . . . . 0 . . . . 32,767

  30. Type Size in Bytes Minimum Value Maximum Value char 1 -128 127 short 2 -32,768 32,767 int 4 -2,147,483,648 2,147,483,647 long 4 -2,147,483,648 2,147,483,647 NOTE: Values given for one machine. Actual sizes are machine-dependent. Some Integral Types

  31. Examples 97 int 40000L long 50000 long 389123487UL uns long 65535U uns int 427l long 53100 long 42513999ul uns long The type of a constant depends on its size unless the type specifier is used U L UL ul LU lu Decimal Constants

  32. Octal Constants • Always begins with 0 • Type specifier may follow • Never contains 8 or 9 • 0372 • = 3·82 + 7·81 +2·80 • Examples • 017 • 0577777L • 01267333l • 01267335 • 0482 (illegal) • 0372

  33. Hexadecimal Constants • Hex digits include 0-9 plus a or A = 10 d or D = 13 b or B = 11 e or E = 14 c or C = 12 f or F = 15 • Examples • 0x2C 0x67A • 0Xac12eL = 6·162 + 7·161 +10·160 • 0x67A

  34. Operatorsizeof DEFINITION C++ has a unary operator named sizeof that yields the size on your machine, in bytes, of its single operand. The operand can be a variable name, or it can be the name of a data type enclosed in parentheses. int age ; cout << “Size in bytes of variable age is “ << sizeof age << endl ; cout << “Size in bytes of type float is “ << sizeof ( float ) << endl ;

  35. The only guarantees made by C++ are . . . 1 = sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long ) 1 <= sizeof ( bool ) <= sizeof ( long ) sizeof ( float ) <= sizeof ( double ) <= sizeof ( long double ) char is at least 8 bits short is at least 16 bits long is at least 32 bits

  36. Floating Point Data Types

  37. Type # Bytes # S.D. Minimum Maximum Positive Value Positive Value float 4 6 1.19E-38 3.4E+38 double 8 15 2.22E-308 1.79E+308 long double 10 18 3.4E-4932 1.1E+4932 *Microsoft uses double NOTE: Values given for one machine. Actual sizes are machine-dependent. See rangeflo.cpp Floating Point Types

  38. Floating Point Constants +/- digits.digits +/- digits.digits E +/- digits • Examples • 6.83 double • 6.83F float • 6.83L long double • 4.35E-9 double

  39. More about Floating Point Types • floating point constants in C++ like 94.6 without a suffix are of type double by default • to obtain another floating point type constant a suffix must be used • the suffix F or f denotes float type, as in 94.6F • the suffix L or l denotes long double, as in 94.6L

  40. Header Files climits and cfloat • contain constants whose values are the maximum and minimum for your machine • such constants are FLT_MAX, FLT_MIN, LONG_MAX, LONG_MIN #include < climits > using namespace std ; . . . cout << “Maximum long is “ << LONG_MAX << endl ; cout << “Minimum long is “ << LONG_MIN << endl ;

  41. Assignment Expressions

  42. Assignment Expression • An assignment • assigns a value to a variable • returns the value stored into the variable • delta = 2 * 12; • 24 is stored in delta • 24 is returned as the result of the assignment expression

  43. Assignment Expression • A C++ expression with • (1) a value and • (2) the side effect of storing the expression value into a memory location

  44. Expression Statement • A statement formed by appending a semicolon to an expression • 23; • 2 * (alpha + beta);

  45. Combined Assignment Operators

  46. C++ Has Combined Assignment Operators int age ; cin >> age ; Write a statement to add 3 to age. age = age+ 3 ; OR, age += 3 ;

  47. Write a statement to subtract 10 from weight int weight ; cin >> weight ; weight = weight - 10 ; OR, weight -= 10 ;

  48. Write a statement to divide money by 5.0 float money ; cin >> money ; money = money/ 5.0 ; OR, money /=5.0 ;

  49. Write a statement to doubleprofits float profits ; cin >> profits ; profits = profits* 2.0 ; OR, profits *= 2.0 ;

  50. Write a statement to raisecost 15% float cost; cin >> cost; cost = cost + cost* .15 ; OR, cost = 1.15 * cost; OR, cost *= 1.15 ;

More Related