Download presentation
Presentation is loading. Please wait.
Published byAntony McCoy Modified over 9 years ago
1
Chapter 2 C++ self study 2nd Semester 1435 -1436 1
2
What makes for a good or bad variable name? myAge PeopleOnTheBus xjk prndl 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
3
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
4
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
5
#include 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 increment cout << "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
6
Output: I am 39 years old You are 39 years old One year passes I am 40 years old You are 40 years old Another year passes I am 40 years old You are 41 years old Let's print it again I am 41 years old You are 41 years old 6
7
OUTPUT STREAM 7
8
Formatting Stream Output Performs formatted and unformatted output I.Display numbers on different width, filling spaces with characters II.Varying precision for floating points III.Formatted text outputs 8
9
Formatting Stream Output iomanip First you must include iomanip #include 9
10
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
11
Example 11
12
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
13
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
14
Example using fixed Note: A call to this function sets the precision for all subsequent output operations until the next precision. 14
15
Significant Digits in Float If we don’t use fixed the function precision will determine the number of digits to the entire number 15
16
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
17
Using showpoint/noshowpoint 17
18
STRING FUNCTIONS 18
19
Size() Determine the size of a string variable 19
20
Clear() Erases the contents of the string, which becomes an empty string (with a length of 0 characters).stringempty stringlength 20
21
compare(string) Compares the value of the string to the sequence of characters specified by its arguments.string Return 0 if thy are the same and -1 if not 21
22
capacity() Returns the size of the storage space currently allocated for the string, expressed in terms of bytes.string 22
23
Append(string ) Extends the string by appending (add) additional characters at the end of its current value:string 23
24
Append( string, int, int) Extends the string by appending (add) additional characters at the end of its current valuestring 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.