6. C++
1.2 Hello World
#include <iostream>
// include iostream package
int main() { // begin main function
std::cout << "Hello world"; // Output
return 0; // main function return
} // end main function
7. C++
1.3 Comments
•Comments are pieces of source code
discarded from the code by the compiler.
•They do nothing, but it allow programmers
to insert notes or descriptions
9. C++
1.4 Includes
• Sentences that begin with a pound sign (#) are
directives for the preprocessor
• In the example the sentence
#include <iostream>
• tells the compiler's preprocessor to include the
iostream standard header file.
10. C++
1.5 Main Function
• The main function is the point by where all C++ programs
begin their execution.
• A function is a program (or code) that performs a task.
int main() { //main function
return 0; // main function return
}
11. C++
1.6 Code Blocks
• Code block is a set of statements that fall
between open and closing brackets: "{" "}".
int main() { //main function (code blocks)
std::cout << "Hello world"; // Output
return 0; // main function return (code blocks)
} // end main function
15. C++
2.1 Variable
• A variable is a named location in memory that is
used to hold a value which may be modified by the
program.
• The general form of a declaration is
datatype variable = initial;
17. C++
2.2 Data Types
1. Boolean
2. Character
3. Integer
4. Floating-Point
5. Casts
6. Variable Locations
18. C++
2.2 Data Types
•C++ has a set of basic data types:
•boolean (bool)
•character (char)
•integer (such as int)
•floating-point (such as double and float)
•with the exception of type void
19. C++
2.2 Data Types
•The basic data types can be modified by
using a signed, unsigned, long, or
short modifier.
20. C++
2.2.1 Boolean
•A Boolean can have one of the two values
true or false (1 or 0).
Type Range Size in Bits
bool 0,1 1
21. C++
2.2.1 Boolean Examples
bool b = true; // b becomes true
int i = false; // int(false) is 0, so i becomes 0
bool b = 4; // bool(4) is true, so b becomes true
22. C++
2.2.2 Character
•A Character type char can hold a character
such as 'A' or '$'
Type Range Size in Bits
char -127 to 127 8
signed char -127 to 127 8
unsigned char 0 to 255 8
23. C++
2.2.2 Character Example
char ch = 'a'; // ch hold ‘a’
•ASCII 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
24. C++
2.2.2 Character
• A few characters have standard names that use
the backslash as an escape character such as “n
t v”.
Character ASCII Description
n NL (LF) newline
t HT horizontal tab
v VT vertical tab
25. C++
2.2.3 Integer
•Integer can store a whole number value such
as 7 or 1024
Type Range Size in Bits
int -32 767 to 32 767 16
signed int -32 767 to 32 767 16
unsigned int 0 to 65 535 16
short int -32 767 to 32 767 16
26. C++
2.2.3 Integer
• Continue
Type Range Size in Bits
signed short int -32 767 to 32 767 16
unsigned short int 0 to 65 535 16
long int -2 147 483 647 to 2 147 483 647 32
signed long int -2 147 483 647 to 2 147 483 647 32
unsigned long int 0 to 4 294 967 295 32
27. C++
2.2.3 Integer Examples
int k = 123; // decimal
int k = 012; // octal (12)8= (10)10
int k = 0x12; // hexadecimal (12)16= (18)10
int k = 'A'; // k hold 65 ASCII code of ‘A’
28. C++
2.2.4 Floating-Point
•Floating-Point can represent real values,
such as 3.14 or 0.01
Type Range Size in Bits
float 1e-37 to 1e+37, 6 digits of precision 32
double *, 10 digits of precision 64
long double *, 10 digits of precision 128
30. C++
2.2.5 Casts
•Casts can be used to make data type
conversion
• Example:
float f = 2.5f; // f hold 2.5
int i = int(f); // i hold 2
int z = (int)f; // i hold 2
32. C++
2.2.6 Variable Locations Example
#include <iostream>
using namespace std;
int i=1; // Global variable
int main() {
int k=2; // Local variable
return 0;
}
36. C++
2.3.1 Assignment
Sign Description Sign Description
= simple assignment &= AND and assign
+= add and assign |=
inclusive OR and
assign
-= subtract and assign ^=
exclusive OR and
assign
*= multiply and assign <<= shift left and assign
/= divide and assign >>= shift right and assign
%= modulo and assign
39. C++
2.3.2 Arithmetic
Sign Description Sign Description
opr1 + opr2 addition. ++opr add 1 to operand before using the value
opr1 - opr2 subtraction. --opr
subtract 1 from operand before using
the value
opr1 * opr2 multiplication opr++ add 1 to operand after using the value
opr1 / opr2 division opr--
subtract 1 from operand after using the
value
opr1 % opr2
remainder (modulo) after dividing
opr1 by opr2.
++opr add 1 to operand before using the value
opr1 + opr2 addition.
42. C++
2.3.3 Comparison
Sign Description Sign Description
== Equal < Less than
!= Different >= Greater or equal than
> Greater than <= Less or equal than
44. C++
2.3.4 Logical
•Logic operators && and || are used
when evaluating two expressions to
obtain a single result.
•Operator ! is equivalent to boolean
operation NOT, it has only one
operand
45. C++
2.3.4 Logical
Sign Description
opr1 && opr2 Conditional "and". true if both operands are true, otherwise false.
opr1 || opr2 Conditional "or". true if either operand is true, otherwise false.
!opr true if opr is false, false if opr is true
46. C++
2.3.4 Logical Example
(ch == 'A' || ch == 'Z')
// ch may Capital 'A' or Capital 'Z'
(income > 5 && income < 10)
/* income must be greater than 5 and
less than 10 */
47. C++
2.3.5 Conditional
•The conditional operator ( ? ) evaluates
an expression and returns a different
value according to the evaluated
expression, depending on whether it is
true or false.
48. C++
2.3.5 Conditional
cond ? res1 : res2
if cond is true, the value is res1, else res2.
Results must be the same type
50. C++
2.3.6 Bitwise
•Bitwise operators modify the
variables considering the bits that
represent the value they store, that
means, their binary representation.
51. C++
2.3.6 Bitwise
Sign Description Sign Description
opr1 & opr2 bitwise AND ~ opr complement
opr1 | opr2 bitwise inclusice OR opr1 << opr2 shift right
opr1 ^ opr2 bitwise exclusive OR (XOR) opr1 >> opr2 shift left
52. C++
2.3.6 Bitwise Example
int x = 7;
// value of x= (7)10 = (00000111)2
x = x << 1;
// value of x= (14)10 = (00001110)2
55. C++
3.1.1 If Statement
•If the boolean expression
evaluates to true, then the if
block of code will be executed.
otherwise skip if block of code.
Expression
Action
true false
Flow Chart
57. C++
3.1.1 If Statement Example
if ( 5 < 10 ) { // if statement
cout<<"Five is now less than ten,
that's a big surprise";
} // end of if statement
58. C++
3.1.2 If-else Statement
•If the boolean expression
evaluates to true, then the if
block of code will be executed,
otherwise else block of code
will be executed.
Flow Chart
Expression
Action1 Action2
true false
60. C++
3.1.2 If-else Statement Example
if ( 5 > 10 ) { // if statement
cout<<"Five is less than ten";
}else{ // if else statement
cout<<"Five is not greater than ten";
}
61. C++
3.1.2 If-else Statement Example 2
if ( x< 0 ) // if statement
cout << x<< " is negative" << endl;
else if ( x> 0 ) // else if statement
cout << x<< " is positive" << endl;
else // else statement
cout << x<< " is zero" << endl;
62. C++
3.1.3 Switch Statement
•The switch statement causes control to be
transferred to one of several statements
depending on the value of an expression.
•Technically, the break statements inside
the switch statement are optional. They
terminate the statement sequence
63. C++
3.1.3 Switch Statement Example Part 1
cout << "Type a, b, or c: ";
cin >> ch; // read user input
switch(ch) // check the value of "ch"
{
case 'a': // if ch equals 'a' or 'A', say so
cout << "You typed a.";
break; // break switch statement
64. C++
3.1.3 Switch Statement Example Part 2
case 'b': cout << "You typed b.";
break;
case 'c': cout << "You typed c";
break;
default: // if ch isn't a, b or c, say so
cout << "You did not type a, b or c.";
}
65. C++
3.2 Iteration
• Repeating identical or similar
tasks
1.While Statement
2.Do Statement
3.For Statement
Flow Chart
66. C++
3.2.1 While Statement
•The while loop iterates while the
condition is true. When the condition
becomes false, program control passes to
the line after the loop code.
80. C++
4 Arrays and Strings
1.Single-Dimension Arrays
2.Multidimensional Arrays
3.String
81. C++
4.1 Single-Dimension Arrays
•Single-dimension arrays are essentially
lists of information of the same type that
are stored in contiguous memory
locations in index order. All arrays have
zero as the index of their first element.
86. C++
4.1.1 Static Arrays Example
int n[5];
cout<<"Enter 5 numbers: ";
for (int i = 0; i < 5; i++) { cin>>n[i]; }
// input all elements of array n
for (int i = 0; i < 5; i++) { cout<<n[i]<<endl; }
// output all elements of array n
87. C++
4.1.2 Array of Char Manipulation
1- import package cstring
#include <cstring>
88. C++
4.1.2 Array of Char Manipulation
Name Function
strcpy(s1, s2) Copies s2 into s1
strcat(s1, s2) Concatenates s2 onto the end of s1
strlen(s1) Returns the length of s1
strcmp(s1, s2) Returns zero if s1 and s2 are the same; less than zero if s1 < s2;
greater than zero if s1 > s2
strchr(s1, ch) Returns a pointer to the first occurence of ch in s1
strstr(s1, s2) Returns a pointer to the first occurence s2 in s1
#include <cstring>
89. C++
4.1.2 Array of Char Manipulation
char name[] = "Lasse";
cout << “name length = " <<
strlen(name) << endl;
// name length = 5
90. C++
4.2 Multidimensional Arrays
•A two-dimensional array can be think as a
table, which will have x number of rows and
y number of columns.
Datatype arrayName [ x ][ y ];
106. C++
5.3.1 Pass by Value
• The Pass by Value method of
passing arguments to a function
copies the actual value of an
argument into the formal
parameter of the function.
107. C++
5.3.1 Pass by Value Example1
void show(int arg1)
{
cout<<“pass by value=”<<arg1;
}
108. C++
5.3.1 Pass by Value Calling Example1
void main()
{
show(5); // print 5 on screen
}
109. C++
5.3.1 Pass by Value Example2
void add(int x, int y)
{
cout<<“add(x,y)=”<<x+y;
}
110. C++
5.3.1 Pass by Value Calling Example2
void main()
{
add(5,6);
// print “add(x,y)=11” on screen
}
111. C++
5.3.2 Pass by Reference
In pass by reference , a
copy of the address of the
actual parameter is stored.
112. C++
5.3.2 Pass by Reference Example1
void addone(int &x)
{
x=x+1;
}
113. C++
5.3.2 Pass by Reference calling
Example1
void main(){
int y=2;
addone(y)
cout<<y; // print 3 on screen
}
114. C++
5.4 Function Return Values
A function may return a value.
The return type is the data
type of the value the
function returns.