Presentation is loading. Please wait.

Presentation is loading. Please wait.

Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.

Similar presentations


Presentation on theme: "Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for."— Presentation transcript:

1 Types Chapter 2

2 C++ An Introduction to Computing, 3rd ed. 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

3 C++ An Introduction to Computing, 3rd ed. 3 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.

4 C++ An Introduction to Computing, 3rd ed. 4 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

5 C++ An Introduction to Computing, 3rd ed. 5 Objects Description Software Objects TypeKindName screen ostream varying cout various prompts string constant student name last first initial string char varying lastName firstName middleInitial keyboard istream varying cin student’s ID int varying idNumber student wage double constant HOURLY_WAGE student’s hours double varying hoursWorked student’s pay double varying pay descriptive label string constant

6 C++ An Introduction to Computing, 3rd ed. 6 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 )

7 C++ An Introduction to Computing, 3rd ed. 7 Algorithm 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, i dNumber, and pay, with descriptive labels. 1. Declare the constant HOURLY_WAGE.

8 C++ An Introduction to Computing, 3rd ed. 8 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.1Fig 2.1

9 C++ An Introduction to Computing, 3rd ed. 9 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

10 C++ An Introduction to Computing, 3rd ed. 10 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

11 C++ An Introduction to Computing, 3rd ed. 11 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

12 C++ An Introduction to Computing, 3rd ed. 12 Integer Literals Decimal integers Sequence of digits without a decimal point Octal integers Sequence of digits beginning with a 0 Base 8 Hexadecimal integers Sequence of digits beginning with an X Base 16

13 C++ An Introduction to Computing, 3rd ed. 13 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

14 C++ An Introduction to Computing, 3rd ed. 14 Reals Values are stored internally in scientific notation A sign for the number Significant digits of the number The power of 10 Sign for the power

15 C++ An Introduction to Computing, 3rd ed. 15 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

16 C++ An Introduction to Computing, 3rd ed. 16 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'

17 C++ An Introduction to Computing, 3rd ed. 17 Characters Escape characters A backslash \ combined with another character hold special meaning CharacterC++ Escape Sequence Newline \n Horizontal tab \t Vertical tab \v Backspace \b Carriage Return \r Form Feed \f Alert (beep) \a

18 C++ An Introduction to Computing, 3rd ed. 18 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

19 C++ An Introduction to Computing, 3rd ed. 19 Identifiers Names given to software objects Rules: Must not be C++ keywords int, if, while, … Must start with a letter (or the underscore _ ) Followed by numerals, letters, underscore Recommendation Use meaningful identifiers Go for longer names, rather than shorter

20 C++ An Introduction to Computing, 3rd ed. 20 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

21 C++ An Introduction to Computing, 3rd ed. 21 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

22 C++ An Introduction to Computing, 3rd ed. 22 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”,...

23 C++ An Introduction to Computing, 3rd ed. 23 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

24 C++ An Introduction to Computing, 3rd ed. 24 Constants Reasons to use constants Improve readability of the source code Facilitate program modification Good programming practice Place all constant declarations at beginning of function where used

25 C++ An Introduction to Computing, 3rd ed. 25 Variables Give a name to a memory location Compiler accesses specific memory location when program uses a given variable Refer to objects in the program for which the value can change Declaration type variableName; // or type variableName = initializer_expression;

26 C++ An Introduction to Computing, 3rd ed. 26 Variables Variables Declaration Can be either initialized or uninitialized... If variable is uninitialized Contents must be considered "garbage value" Examples: int age = 18; double GPA = 3.25, credits; char letterGrade = ‘A’; bool ok, done = false;

27 C++ An Introduction to Computing, 3rd ed. 27 int Representation Integers are often represented in the twos-complement format, High-order bit indicates the number’s sign: 2 10 = 0000000000000010 2 1 10 = 0000000000000001 2 0 10 = 0000000000000000 2 -1 10 = 1111111111111111 2 -2 10 = 1111111111111110 2 We show 16 bits, but 32 or 64 are common.

28 C++ An Introduction to Computing, 3rd ed. 28 Twos-Complement Algorithm to find twos-complement representation of a negative number: 1. Select your number (e.g., -12) 2. Represent its absolute value in binary: (0000000000001100) 3. Invert the bits (1111111111110011) 4. Add 1 (1111111111110100)

29 C++ An Introduction to Computing, 3rd ed. 29 unsigned Objects Some objects have values that are never negative, C++ provides the unsigned type: 0000000000000000 2 = 0 10 0000000000000001 2 = 1 10 0000000000000010 2 = 2 10... 1111111111111110 2 = 65534 10 1111111111111111 2 = 65535 10 No sign bit, numbers can be twice as big.

30 C++ An Introduction to Computing, 3rd ed. 30 int vs. unsigned Using 32 bits, int values range from -2 31 (-2147483648) to 2 31 -1 (2147483647), unsigned values range from 0 to 2 32 -1 (4294967295). An int value “loses” one of its bits to the sign, Maximum int value is about half of the maximum unsigned value.

31 C++ An Introduction to Computing, 3rd ed. 31 double Objects Real values are often represented in 64 bits Use the IEEE floating point standard: mantissa (52 bits) exponent (11 bits) sign (1 bit)

32 C++ An Introduction to Computing, 3rd ed. 32 double Objects Overflow Exponent is too large Not enough bits to represent Underflow Number is so small Not enough bits to represent negative exponent

33 C++ An Introduction to Computing, 3rd ed. 33 char and String Objects Characters represented internally with binary codes 8 bits – only 128 characters Strings stored as a sequence of these binary codes

34 C++ An Introduction to Computing, 3rd ed. 34 char and String Objects Unicode uses 16 bit codes Possible to represent more than 65,000 characters Can include special characters

35 C++ An Introduction to Computing, 3rd ed. 35 Booleans Only two values true and false true stored as 1 Anything non zero will be interpreted as true false stored as 0 Could be stored in a single bit Usually stored in a word or byte

36 C++ An Introduction to Computing, 3rd ed. 36 OBJECTive Thinking: Attribute Variables Represent complicated objects in a program by using a class Must first design and build the class Then use that class as a type to declare the object(s)

37 C++ An Introduction to Computing, 3rd ed. 37 Class Structure Declaration: class className { public: // declare operations private: // declare attributes }; Must be a valid identifier C++ Keywords

38 C++ An Introduction to Computing, 3rd ed. 38 Attribute Variables Attribute variables are usually specified as private class Sphere { public:... Sphere-operation declarations go here private: double myRadius, myDensity, myWeight; }; All three sphere attributes now wrapped in a single package – the Sphere object. This declaration stored in the file "sphere.h"

39 C++ An Introduction to Computing, 3rd ed. 39 Declaring Sphere Objects Specify the include of the "Sphere.h" file Declare as many sphere objects as needed. #include "Sphere.h" int main() { Sphere oneSphere, anotherSphere;... }

40 C++ An Introduction to Computing, 3rd ed. 40 Declaring Sphere Objects Each of the sphere objects will have space within them for the three sphere attributes

41 C++ An Introduction to Computing, 3rd ed. 41 Declaring Sphere Objects A class object itself can contain class objects as an attribute


Download ppt "Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for."

Similar presentations


Ads by Google