47
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood so that they can be maintained C++ has two conventions for comments // single line comment (preferred) /* long comment */ (save for debugging) Typical uses Identify program and who wrote it Record when program was written Add descriptions of modifications

1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

Embed Size (px)

Citation preview

Page 1: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

1

CommentsAllow prose or commentary to be included in programImportance

Programs are read far more often than they are written

Programs need to be understood so that they can be maintained

C++ has two conventions for comments // single line comment (preferred) /* long comment */ (save for debugging)

Typical uses Identify program and who wrote it Record when program was written Add descriptions of modifications

Page 2: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

2

Fundamental C++ Objects

C++ has a large number of fundamental or built-in object typesThe fundamental object types fall into one of three categories

Integer objects Floating-point objects Character objects

11.28345

Z5

P3.14

Page 3: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

3

Integer Object Types

The basic integer object type is int The size of an int depends on the machine and the

compiler On PCs it is normally 16 or 32 bits

Other integers object types short: typically uses less bits long: typically uses more bits

Different types allow programmers to use resources more efficientlyStandard arithmetic and relational operations are available for these types

Page 4: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

4

Integer Constants

Integer constants are positive or negative whole numbersInteger constant forms

Decimal Octal (base 8)

Digits 0, 1, 2, 3, 4, 5, 6, 7 Hexadecimal (base 16)

Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a , b, c, d, e, f, A, B, C, D, E, F

Consider 31 oct and 25 dec

Page 5: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

5

Decimal Constants

Examples 97 40000L 50000 23a (illegal)

The type of the constant depends on its size, unless the type specifier is used

L or l indicates long integer

Page 6: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

6

Character Object Types

Character type char is related to the integer typesCharacters are encoded using a scheme where an integer represents a particular characterASCII is the dominant encoding scheme

Examples ' ' encoded as 32 '+' encoded as 43 'A' encoded as 65 'Z' encoded as 90 'a' encoded as 97 'z' encoded as 122

Appendix A gives the complete ASCII character set

Page 7: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

7

Character Operations

Arithmetic and relational operations are defined for characters types

'a' < 'b' is true '4' > '3' is true '6' <= '2' is false

Page 8: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

8

Character ConstantsExplicit (literal) characters within single quotes

'a','D','*'

Special characters - delineated by a backslash \

Two character sequences (escape codes)

Some important special escape codes \t denotes a tab \n denotes a new line \\ denotes a backslash \' denotes a single

quote \" denotes a double quote

'\t' is the explicit tab character, '\n' is the explicit new line character, and so on

Page 9: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

9

Literal String Constants

A literal string constant is a sequence of zero or more characters enclosed in double quotes

"We are even loonier than you think" "Rust never sleeps\n" "Nilla is a Labrador Retriever"

Not a fundamental type

Page 10: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

10

Floating-Point Object Types

Floating-point object types represent real numbers Integer part Fractional part

The number 108.1517 breaks down into the following parts 108 - integer part 1517 - fractional part

C++ provides three floating-point object types float double long double

Page 11: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

11

Floating-Point Constants

Standard decimal notation134.1230.15F

Standard scientific notation1.45E6 0.979e-3L

When not specified, floating-point constants are of type double

F or f indicates single precision floating point value

L or l indicates long double floating point value

Page 12: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

12

Names

Used to denote program values or components

A valid name is a sequence of Letters (upper and lowercase) Digits

A name cannot start with a digit Underscores

A name should not normally start with an underscore

Names are case sensitive MyObject is a different name than MYOBJECT

There are two kinds of names Keywords Identifiers

Page 13: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

13

Keywords

Keywords are words reserved as part of the language int, return, float, double

They cannot be used by the programmer to name things

They consist of lowercase letters only

They have special meaning to the compiler

Page 14: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

14

Identifiers

Identifiers should be

Short enough to be reasonable to type (single word is norm) Standard abbreviations are fine (but only standard

abbreviations)

Long enough to be understandable When using multiple word identifiers capitalize the

first letter of each word

Examples Min Temperature CameraAngle CurrentNbrPoints

Page 15: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

15

Definitions

All objects that are used in a program must be defined

An object definition specifies Type Name

General definition form

Our convention is one definition per statement!

Type Id, Id, ..., Id;

Knowntype

List of one ormore identifiers

Page 16: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

16

Examples

char Response;

int MinElement;

float Score;

float Temperature;

int i;

int n;

char c;

float x;

Objects are uninitialized with this definition form

(Value of a object is whatever is in itsassigned memory location)

Page 17: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

17

Arithmetic Operators

Common Addition + Subtraction - Multiplication * Division / Mod %

Note No exponentiation operator Single division operator Operators are overloaded to work with more than

one type of object

Write m*x + bnot mx + b

Page 18: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

18

Integer Division

Integer division produces an integer result Truncates the result

Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3

Page 19: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

19

Mod

Produces the remainder of the division

Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4

Page 20: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

20

Operators and Precedence

Consider mx + b

Consider m*x + b which of the following is it equivalent to

(m * x) + b m * (x + b)

Operator precedence tells how to evaluate expressions

Standard precedence order () Evaluate first, if nested

innermostdone first

* / % Evaluate second. If there are several,

then evaluate from left-to-right

+ - Evaluate third. If there are several,

then evaluate from left-to-right

Page 21: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

21

Operator Precedence

Examples 20 - 4 / 5 * 2 + 3 * 5 % 4

(4 / 5)

((4 / 5) * 2)

((4 / 5) * 2) (3 * 5)

((4 / 5) * 2) ((3 * 5) % 4)

(20 -((4 / 5) * 2)) ((3 * 5) % 4)

(20 -((4 / 5) * 2)) + ((3 * 5) % 4)

Page 22: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

22

Defining and Initializing

When an object is defined using the basic form, the memory allotted to it contains random information

Better idea to specify its desired value at the same time Exception is when the next statement is an

extraction for the object

Remember our convention of one definition per statement!

Page 23: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

23

Examples

int FahrenheitFreezing = 32;char FinalGrade = 'A';cout << "Slope of line: ";float m;cin >> m;cout << "Intercept: ";float b;cin >> b;cout << "X value of interest: ";float x;cin >> x;float y = (m * x) + b;

Page 24: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

24

Modifying Objects

Operators and Expressions

Chapter 3

JPC and JWD © 2002 McGraw-Hill, Inc.

Page 25: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

25

Memory Depiction

float y = 12.5;12.5y

1001100210031004

Page 26: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

26

Memory Depiction

float y = 12.5;

int Temperature = 32;12.5

32

y

Temperature

100110021003100410051006

Page 27: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

27

Memory Depiction

float y = 12.5;

int Temperature = 32;

char Letter = 'c';

12.5

32'c'

y

TemperatureLetter

1001100210031004100510061007

Page 28: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

28

Memory Depiction

float y = 12.5;

int Temperature = 32;

char Letter = 'c';

int Number;

12.5

32'c'

y

TemperatureLetter

1001100210031004100510061007

-Number 10081009

Page 29: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

29

Assignment Statement

Basic form object = expression ;

Celsius = (Fahrenheit - 32) * 5 / 9;

y = m * x + b;

Action Expression is evaluated Expression value stored in object

Target becomes source

Page 30: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

30

Definition

int NewStudents = 6; 6NewStudents

Page 31: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

31

Definition

int NewStudents = 6;

int OldStudents = 21;

6

21

NewStudents

OldStudents

Page 32: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

32

Definition

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

6

21

NewStudents

OldStudents

-TotalStudents

Page 33: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

33

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

6

21

NewStudents

OldStudents

?TotalStudents

Page 34: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

34

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

6

21

NewStudents

OldStudents

27TotalStudents

Page 35: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

35

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

OldStudents = TotalStudents;

6

?

NewStudents

OldStudents

27TotalStudents

Page 36: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

36

Assignment Statement

int NewStudents = 6;

int OldStudents = 21;

int TotalStudents;

TotalStudents = NewStudents + OldStudents;

OldStudents = TotalStudents;

6

27

NewStudents

OldStudents

27TotalStudents

Page 37: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

37

Consider

int Value1 = 10; 10Value1

Page 38: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

38

Consider

int Value1 = 10;

int Value2 = 20;

10

20

Value1

Value2

Page 39: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

39

Consider

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

10

20

Value1

Value2

10Hold

Page 40: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

40

Consider

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

?

20

Value1

Value2

10Hold

Page 41: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

41

Consider

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

20

20

Value1

Value2

10Hold

Page 42: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

42

Consider

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

Value2 = Hold;

20

?

Value1

Value2

10Hold

Page 43: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

43

Consider

int Value1 = 10;

int Value2 = 20;

int Hold = Value1;

Value1 = Value2;

Value2 = Hold;

We swapped the values of objects Value1 and Value2 using Hold as temporary holder for Value1’s starting value!

20

10

Value1

Value2

10Hold

Page 44: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

44

Incrementing

int i = 1; i 1

Page 45: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

45

Incrementing

int i = 1;

i = i + 1;

Assign the value of expression i + 1 to i

Evaluates to 2

i 1

2i

Page 46: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

46

Const Definitions

Modifier const indicates that an object cannot be changed

Object is read-only

Useful when defining objects representing physical and mathematical constants

const float Pi = 3.1415;

Value has a name that can be used throughout the program

const int SampleSize = 100;

Makes changing the constant easy Only need to change the definition and recompile

Page 47: 1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood

47

Assignment Conversions

Floating-point expression assigned to an integer object is truncated

Integer expression assigned to a floating-point object is converted to a floating-point value

Considerfloat y = 2.7;int i = 15;int j = 10;i = y; // i is now 2cout << i << endl; y = j; // y is now 10.0cout << y << endl;