Types Chapter 2 Objectives Observe types provided by C++ • Literals of these types Explain syntax rules for identifier names Study variables and constants • • • • What they are How they differ How to declare How to use Investigate internal representations First look at class attribute variables C++ An Introduction to Computing, 3rd ed. 2 Problem We are given the task of writing a program to help the payroll office. They compute pay for university student workers Students are all paid an hourly rate of $6.75 Consider what behavior we desire for the program. C++ An Introduction to Computing, 3rd ed. 3 Behavior For student payroll calculation: Enter student name (last, first, initial): Smart, Osgood J. Enter ID number: 123456 Enter hours worked: 9.99 Student: Osgood J. Smart ID: 123456 Pay = $99.99 C++ An Introduction to Computing, 3rd ed. 4 Operations Display string (prompts, labels, name) Read a string (lastName, firstName) Read a char (middleInitial) Read an integer (idNumber) Read a real value (hoursWorked) Compute pay = hoursWorked * HOURLY_WAGE Display an integer (idNumber) Display a real value (pay) C++ An Introduction to Computing, 3rd ed. 5 Algorithm 1. Declare the constant HOURLY_WAGE. 2. Display to cout a prompt for the student’s name (last, first, middle initial). 3. Read two strings and a character from cin into lastName, firstName, middleInitial. 4. Display to cout a prompt for the student’s id number. 5. Read an integer from cin into idNumber. 6. Display to cout a prompt for the student’s hours. 7. Read a real value from cin into hoursWorked. 8. Compute pay = hoursWorked * HOURLY_WAGE. 9. Display firstName, lastName, middleInitial, idNumber, and pay, with descriptive labels. C++ An Introduction to Computing, 3rd ed. 6 Coding, Execution, Testing Create a program stub • Opening documentation • Compiler directives for library includes • An empty main function Convert each step of algorithm into code • Add declaration for each object not already declared • Declaration includes object type and name. Observe source code, Fig 2.1 C++ An Introduction to Computing, 3rd ed. 7 Types and Declarations Fundamental Types Integers (whole numbers, negatives) • int Integer variations • short, long, unsigned Reals (fractional numbers) • float, double, long double Characters (letters, digits, symbols, punctuation) • char Booleans (logical values, true and false) • bool C++ An Introduction to Computing, 3rd ed. 8 Integer Memory used for an int depends on word size used by the hardware • Usually 16, 32, or 64 bits used 16 Bits (2 bytes) – short int • Range -32768 … 32767 32bits (4 bytes) – long int (or long) • Range -2137483648 … 2137483647 One bit used for the sign C++ An Introduction to Computing, 3rd ed. 9 Integer Integers can be specified as unsigned • Then the sign bit not needed • Gives larger positive range unsigned short (16 bits) • Range 0 … 65535 unsigned long (32 bits) • Range 0 … 4294967295 C++ An Introduction to Computing, 3rd ed. 10 Reals float • Usually a 32-bit value (4 bytes) double • A 64-bit value (8 bytes) long double • A 96- or 128-bit value The programmer should choose which type based on degree of precision desired for the object C++ An Introduction to Computing, 3rd ed. 11 Reals Values are stored internally in scientific notation 1.23 10 • • • • 4 A sign for the number Significant digits of the number The power of 10 Sign for the power C++ An Introduction to Computing, 3rd ed. 12 Reals Literal values • A sequence of digits with leading sign, containing a decimal point • Scientific notation – any one of the following forms 0.12e11 1.2E10 12.0E9 12.e9 12E9 C++ An Introduction to Computing, 3rd ed. 13 Characters char type Represents individual characters • See ASCII character set in Appendix A Characters represented in memory by numeric values Character literals • Characters enclosed in single quotes 'X' '7' '>' 'e' C++ An Introduction to Computing, 3rd ed. 14 Characters Escape characters • A backslash \ combined with another character hold special meaning Character C++ Escape Sequence Newline \n Horizontal tab \t Vertical tab \v Backspace \b Carriage Return \r Form Feed \f Alert (beep) \a C++ An Introduction to Computing, 3rd ed. 15 Strings Related to characters • A sequence of characters • Enclosed in double quotes "Hi Mom" • Can include escape characters "\nThe answer is " Warning • "A" is a string literal • 'A' is a character literal C++ An Introduction to Computing, 3rd ed. 16 Identifiers C++ is case sensitive • firstName is not the same as firstname Typical usage • Constants are all caps PI • Variables • Start with lower case • Capitalize first letter of successive words monthlyElectricCharge C++ An Introduction to Computing, 3rd ed. 17 Object Categories There are three kinds of objects: Literals: • unnamed objects • having a value • (0, -3, 2.5, 2.998e8, ‘A’, “Hello\n”, ...) Variables: • named objects • values can change during program execution Constants: • named objects • values do not change during program execution C++ An Introduction to Computing, 3rd ed. 18 Literals int literals are whole numbers: -27, 0, 4, +4 double literals are real numbers, and can be: • fixed-point: -0.333, 0.5, 1.414, ... • floating-point: 2.998e8, 0.2998e9, ... There are just two bool literals: false, true char literals are single ASCII characters: ‘A’, ‘a’, ‘9’, ‘$’, ‘?’, ... string literals are ASCII character sequences: “Hello”, “Goodbye”, “Goodbye\n”, ... C++ An Introduction to Computing, 3rd ed. 19 Constants Declaration of software objects that remain constant const double HOURLY_WAGE = 6.75; Rules • const is a keyword • Specify type • Specify the name (caps recommended) • Must be initialized with value at declaration C++ An Introduction to Computing, 3rd ed. 20
© Copyright 2026 Paperzz