chapter 2 c++ self study 2nd semester 1435 -1436 1

24
Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Upload: antony-mccoy

Post on 18-Jan-2016

231 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Chapter 2

C++

self study

2nd Semester 1435 -1436

1

Page 2: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

What makes for a good or bad variable name?

• A good variable name tells you what the variable is for; a bad variable name has no information. myAge and PeopleOnTheBus are good variable names, but xjk and prndl are probably less useful.

2

Page 3: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

What happens if I assign a number with a decimal point to an integer rather than to a float?

• Consider the following line of code:• int Number = 5.4;• A good compiler will issue a warning, but the assignment is

completely legal. The number you've assigned will be truncated into an integer. Thus, if you assign 5.4 to an integer variable, that variable will have the value 5. Information will be lost, however, and if you then try to assign the value in that integer variable to a float variable, the float variable will have only 5.

3

Page 4: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

• you can write

x = 35; // ok• but you can't legally write

35 = x; // error • if you have a variable, C, and you want to increment it, you

would use this statement:

C++; // Start with C and increment it.• This statement is equivalent to the more verbose statement

C = C + 1;• which you learned is also equivalent to the moderately verbose

statement• C += 1;

4

Page 5: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

#include <iostream>Using namespace std ;int main() { int myAge = 39; // initialize two integers int yourAge = 39; cout << "I am: " << myAge << " years old.\n";cout << "You are: " << yourAge << " years old\n";myAge++; // postfix increment++yourAge; // prefix incrementcout << "One year passes...\n";cout << "I am: " << myAge << " years old.\n";cout << "You are: " << yourAge << " years old\n";cout << "Another year passes\n";cout << "I am: " << myAge++ << " years old.\n";cout << "You are: " << ++yourAge << " years old\n";cout << "Let's print it again.\n";cout << "I am: " << myAge << " years old.\n";cout << "You are: " << yourAge << " years old\n";return 0; }

5

Page 6: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Output: I am 39 years oldYou are 39 years oldOne year passesI am 40 years oldYou are 40 years oldAnother year passesI am 40 years oldYou are 41 years oldLet's print it againI am 41 years oldYou are 41 years old

6

Page 7: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

OUTPUT STREAM

7

Page 8: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Formatting Stream Output

• Performs formatted and unformatted outputI. Display numbers on different width , filling spaces with

charactersII. Varying precision for floating pointsIII. Formatted text outputs

8

Page 9: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Formatting Stream Output

• First you must include iomanip • #include <iomanip>

9

Page 10: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Setting the Width

• You can use the width(int)or setw(int) function to set the minimum width for printing a value.• If the printed value is shorter than the minimum

width there will be a padding with a space otherwise it will be printed as it is• This function works ONLY for the next insertion

command

10

Page 11: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Example

11

Page 12: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Setting the Fill Character• Use the fill(char) or setfill(char) function to

set the fill character.

• The character remains as the fill character until set again.

12

Page 13: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Significant Digits in Float

• Function precision(int) to set the number of significant digits (to determine number of digits to be displayed)

If we use fixed the function precision will determine the number of digits to the right of the decimal point

13

Page 14: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Example using fixed

Note: A call to this function sets the precision for all subsequent output operations until the next precision.

14

Page 15: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Significant Digits in Float

If we don’t use fixed the function precision will determine the number of digits to the entire number

15

Page 16: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Using showpoint/noshowpoint• showpoint specify that floating-point numbers (even for

whole numbers) should be output with a decimal point, even if they’re zeros. • Following the decimal point, as many digits as necessary

are written to match the precision.• This setting is reset with stream manipulator noshowpoint.• When the showpoint manipulator is not set, the decimal

point is only written for non-whole numbers.

16

Page 17: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Using showpoint/noshowpoint

17

Page 18: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

STRING FUNCTIONS

18

Page 19: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Size()• Determine the size of a string variable

19

Page 20: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Clear()• Erases the contents of the string, which becomes an

empty string (with a length of 0 characters).

20

Page 21: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

compare(string)• Compares the value of the string to the sequence of

characters specified by its arguments.• Return 0 if thy are the same and -1 if not

21

Page 22: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

capacity()• Returns the size of the storage space currently allocated for

the string, expressed in terms of bytes.

22

Page 23: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Append(string )• Extends the string by appending (add) additional characters at

the end of its current value:

23

Page 24: Chapter 2 C++ self study 2nd Semester 1435 -1436 1

Append( string, int, int)• Extends the string by appending (add) additional characters at

the end of its current value• OB.append (str, from index ,how many characters); • EX:

24

str1.append(str2,4,5);

Add to str1 the string str2 from the index 4 and take 5 characters