self-study

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
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
• 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
#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 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
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
OUTPUT STREAM
7
Formatting Stream Output
• Performs formatted and unformatted output
I.
II.
III.
Display numbers on different width , filling spaces with
characters
Varying precision for floating points
Formatted text outputs
8
Formatting Stream Output
• First you must include iomanip
• #include <iomanip>
9
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
Example
11
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
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
Example using fixed
Note: A call to this function
sets the precision for all
subsequent output
operations until the next
precision.
14
Significant Digits in Float
If we don’t use fixed the function precision
will determine the number of digits to the entire
number
15
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
Using showpoint/noshowpoint
17
STRING FUNCTIONS
18
Size()
• Determine the size of a string variable
19
Clear()
• Erases the contents of the string, which becomes an empty
string (with a length of 0 characters).
20
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
capacity()
• Returns the size of the storage space currently allocated for
the string, expressed in terms of bytes.
22
Append(string )
• Extends the string by appending (add) additional characters at
the end of its current value:
23
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:
str1.append(str2,4,5);
24
Add to str1 the string str2 from the index 4 and
take 5 characters