C++Mohamed Loey
C++
C++Contents
1.Basic Information
2.Expressions
3.Statements
4.Arrays and Strings
5.Functions
C++
1 Basic Information
1.Program
2.Hello World
3.Comments
4.Includes
5.Main Function
6.Namespaces
C++
1.1 Program
•A program is a set of human-readable
instructions for the computer to carry
out.
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
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
C++
1.3 Comments Example
// line comment
/* block comment */
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.
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
}
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
C++
1.7 Namespaces
•Namespaces allow to group a set of
global classes, objects and/or
functions under a name.
C++
1.7 Namespaces Example
#include <iostream>
using namespace std; //namespace
int main () {
cout << "Hello world!";
return 0;
}
C++
2 Expressions
1.Variables
2.Data Types
3.Operators
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;
C++
2.1 Variable Example
int i, j, k;
char ch;
double current_balance;
C++
2.2 Data Types
1. Boolean
2. Character
3. Integer
4. Floating-Point
5. Casts
6. Variable Locations
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
C++
2.2 Data Types
•The basic data types can be modified by
using a signed, unsigned, long, or
short modifier.
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
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
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
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
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
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
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
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’
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
C++
2.2.4 Floating-Point
float f = 1.23; // f hold 1.23
float f = 2.0f; // f hold 2.0
double d = .123; // d hold .123
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
C++
2.2.6 Variable Locations
•Variables can be located inside functions
(local variables)
•Variables outside all functions (global
variables)
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;
}
C++
2.3 Operators
1.Assignment
2.Arithmetic
3.Comparison
4.Logical
5.Conditional
6.Bitwise
C++
2.3 Operators
•C++ is rich in built-in operators. It
places significantly more importance on
operators than do most other computer
languages.
C++
2.3.1 Assignment
•The assignment operator assigns a
value to a variable.
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
C++
2.3.1 Assignment Example
int i, j; // i:? j:?
i = 1; // i:1 j:?
j = 2; // i:1 j:2
i += j; // i:3 j:2
j *= 4; // i:3 j:8
C++
2.3.2 Arithmetic
•Operations of addition, subtraction,
multiplication, division and
remainder (modulo)
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.
C++
2.3.2 Arithmetic Example
int i; // i:?
i = 1; // i:1
i++; // i:2
i = 11 % 3; // i:2
i = 13 / 4; // i:3
C++
2.3.3 Comparison
•C++ standard specifies that the
result of a relational operation is
a bool value that can only
be true or false.
C++
2.3.3 Comparison
Sign Description Sign Description
== Equal < Less than
!= Different >= Greater or equal than
> Greater than <= Less or equal than
C++
2.3.3 Comparison Example
(1 == 2) // false
(1 != 2) // true
int i = 1;
int j = 4;
(i <= j) // true
(i > j) // false
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
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
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 */
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.
C++
2.3.5 Conditional
cond ? res1 : res2
if cond is true, the value is res1, else res2.
Results must be the same type
C++
2.3.5 Conditional Example
1==2 ? 1 : 2
// returns 2 since 1 is not equal to 2
C++
2.3.6 Bitwise
•Bitwise operators modify the
variables considering the bits that
represent the value they store, that
means, their binary representation.
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
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
C++
3 Statements
1.Conditional (Selection)
2.Iteration (Loop)
3.Jump
C++
3.1 Conditional
1.If Statement
2.If-else Statement
3.Switch Statement
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
C++
3.1.1 If Statement
if(Condition)
{
// Action
}
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
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
C++
3.1.2 If-else Statement
if(Condition)
{
// Action 1
}else{
// Action 2
}
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";
}
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;
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
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
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.";
}
C++
3.2 Iteration
• Repeating identical or similar
tasks
1.While Statement
2.Do Statement
3.For Statement
Flow Chart
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.
C++
3.2.1 While Statement
while (Condition)
{
// Action
}
C++
3.2.1 While Statement
int x=0;
while (x<=5)
{
cout<<x<<endl; // action
x++; // increment
}
Console Output
0
1
2
3
4
5
C++
3.2.2 Do Statement
•Like a while statement, except that it
tests the condition at the end of the loop
body.
•Execute at least one time
C++
3.2.2 Do Statement
do{
// Action
}while( Condition );
C++
3.2.2 Do Statement
int x=0;
do{
cout<<x<<endl; //action
x++; // increment
} while (x<=5);
Console Output
0
1
2
3
4
5
C++
3.2.3 For Statement
•Execute a sequence of statements
multiple times and abbreviates the code
that manages the loop variable.
C++
3.2.3 For Statement
for(initialization ; condition; increment)
{
// Action
}
C++
3.2.3 For Statement
for(int x=0 ; x<=5; x++)
{
cout<<x<<endl; // Action
}
Console Output
0
1
2
3
4
5
C++
3.3 Conditional
1.Break Statement
2.Continue Statement
3.Switch Statement
C++
3.3.1 Break Statement
•Terminates the loop or switch statement
and transfers execution to anthor
statement.
C++
3.3.1 Break Statement
for(int x=0 ; x<=5; x++)
{
if(x==3) break;
cout<<x<<endl; // Action
}
Console Output
0
1
2
C++
3.3.2 Continue Statement
•Causes the loop to skip the remainder of
its body and immediately retest its
condition prior to reiterating.
C++
3.3.2 Continue Statement
for(int x=0 ; x<=5; x++)
{
if(x==3) continue;
cout<<x<<endl; // Action
}
Console Output
0
1
2
4
5
C++
4 Arrays and Strings
1.Single-Dimension Arrays
2.Multidimensional Arrays
3.String
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.
C++
4.1 Single-Dimension Arrays
1.Static Arrays
2.Array of Char Manipulation
C++
4.1.1 Static Arrays
Datatype variable[size] = { initials };
C++
4.1.1 Static Arrays Example
int x[5] = {1, 2, 5, 3, 6};
C++
4.1.1 Static Arrays Example
int x[5];
x[0]=1;
x[1]=2;
x[2]=5;
x[3]=3;
x[4]=6;
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
C++
4.1.2 Array of Char Manipulation
1- import package cstring
#include <cstring>
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>
C++
4.1.2 Array of Char Manipulation
char name[] = "Lasse";
cout << “name length = " <<
strlen(name) << endl;
// name length = 5
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 ];
C++
4.2 Multidimensional Arrays
int a[2][3];
C++
4.2 Multidimensional Arrays
int test[2][3]
= { {2, -5, 6},
{4, 0, 9}};
C++
4.2 Multidimensional Arrays Input
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 3; ++j) {
cout<<"Enter your number = ";
cin>> test[i][j];
} }
C++
4.2 Multidimensional Arrays Input
for(int i = 0; i < 2; ++i) {
for(int j = 0; j < 3; ++j) {
cout<< test[i][j]<<endl;
} }
C++
4.3 String
String is sequences of characters
C++
4.3 String
char str1[10] = "Hello";
C++
4.3 String Example
char str2[10] = "World";
char str3[10];
strcpy( str3, str2);
cout <<str3<< endl; // str3=“World”
C++
4.3 String Class
1- import package string
#include <string>
C++
4.3 String Class Example
string str1 = "Hello";
string str2 = "World";
string str3;
str3 = str1 + str2; // str3=“Hello World”
cout << "str1 + str2 : " << str3 << endl;
C++
5 Functions
1.Function Definition
2.Function Calling
3.Function Arguments
4.Function Return Values
C++
5.1 Function Definition
A function is a block of instructions
that is executed when it is called
from some other point of the
program.
C++
5.1 Function Definition
Datatype function-name (arguments)
{
statement;
}
C++
5.1 Function Definition
void show() // function show
{
cout<<“Hello World”;
}
C++
5.2 Function Calling
void main() //main function
{
show(); // show function call
}
C++
5.3 Function Arguments
1- Pass by Value
2- Pass by Reference
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.
C++
5.3.1 Pass by Value Example1
void show(int arg1)
{
cout<<“pass by value=”<<arg1;
}
C++
5.3.1 Pass by Value Calling Example1
void main()
{
show(5); // print 5 on screen
}
C++
5.3.1 Pass by Value Example2
void add(int x, int y)
{
cout<<“add(x,y)=”<<x+y;
}
C++
5.3.1 Pass by Value Calling Example2
void main()
{
add(5,6);
// print “add(x,y)=11” on screen
}
C++
5.3.2 Pass by Reference
In pass by reference , a
copy of the address of the
actual parameter is stored.
C++
5.3.2 Pass by Reference Example1
void addone(int &x)
{
x=x+1;
}
C++
5.3.2 Pass by Reference calling
Example1
void main(){
int y=2;
addone(y)
cout<<y; // print 3 on screen
}
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.
C++
5.4 Function Return Values Example1
int add (int x, int y)
{
return x+y;
}
C++
5.3.2 Pass by Reference calling
Example1
void main(){
Int a=1,b=4;
cout<<add(a,b);
// print 5 on screen
}
Thanks For Listening
Mohamed Loey

C++ Programming Language