Goal
Staff
Products
Contact us
01211992678 www.net3lem.com
01211992674 https://www.facebook.com/net3lem
C++ Course
Alaa Ramadan
Net3lem
History Of Programming Languages
// CODE
// my first program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!" ;
return 0;
}
/////////////////OR//////////////////////////////////
int main () { cout << "Hello World!" ; return 0; }
Output
Hello World!
Physical Memory
Variable
a
Functions
Divide your Code
Functions
A function is a group of statements that together perform a task
C++ program has at least one function which is main(),
divide up your code into functions each function performs a specific task.
Defining a Function:
function's name,
return type,
parameters.
actual body of the function.
return_type function_name( parameter list )
{
body of the function
}
Example:
Calling a Function:
return_type function_name( parameter list );
int max(int num1, int num2);
int max(1, 2);
Call Type
CALL BY VALUE
This method copies the actual value
of an argument into the formal
parameter of the function. In this
case, changes made to the
parameter inside the function have
no effect on the argument.
CALL BY REFERENCE
This method copies the reference
of an argument into the formal
parameter. Inside the function, the
reference is used to access the
actual argument used in the call.
This means that changes made to
the parameter affect the argument.
/passing parameters by reference
DefaultValuesfor
Parameters:
Total value is :300
Total value is :120
Functions with no type. The use of void.
Overloaded functions.
Strings
#include<string>
To initialize a variable of type string:
string s1=“hello”
string s2=“Net3lem”;
s1=s2;
s3=s1+s2;
s1.swap(s2);
S1=Net3lm
S2=hello
S1.size() or s1.length() get length of string
string library
Files manipulation
#include<fstream>
Including Library
#include<fstream>
Members of fstream are:
 Ifstream
 Ofstream
 Fstream
ofstream out (“name of the file with its extension”);
ifstream in (“name of the file with its extension”);
fstream both (“name of the file with its extension”);
Write on file Example
First we need to make an output file stream so:
Ofstream out (“file.txt”);
Then we can write by this way
Out<<“Welcome”<<“ ”<<“In”<<“ ”<<“Net3lem”;
Append on file
In ofstream we make a new file with this name and begin to write on it
What if I want to append on the file ?!
The solution : >> Ios:: app
Ofstream out (“file.txt” , ios::app);
Open() / close()
Ofstream out;
Out.open(“file.txt”,ios::app);
If(!out)
cout<<“File couldn’t be open”;
Out.close();
Read from a file
Ifstream In;
In.open(“file.txt”);
Int x,z;
Char y;
In>>x>>y>>z;
I must know the format of the file
Getline
Getline (name of ifstream , name of string,stopping character);
Ex:
getline(index,text,'|');
Eof()
while(!index.eof())
{
Statement 1;
Statmenet 2;
}
Bubble Sort
Intro
The Bubble sorting works at this way:
First: the program will compare between the first element & the second element
then between the first & the third then between the first & the forth then between
the first & the fifth “every time we do swapping if the condition met”
Second: the program will compare between the second & the third then between
the second & the forth then between the second & the fifth “every time we do
swapping if the condition met”
We repeat this till we reach the last element in the array
Implementation
The array before sorting is: (45,30,90,5,70)
The array after sorting is: (5,30,45,70,90)
We need two loops to implement this method
The first for loop will get the first element that we want to compare so we need
another for loop “the second one” to get the another elements from the array
Code
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(array[i]>array[j])
{
int temp=array[i]; //swap
array[i]=array[j];
array[j]=temp;
}}}
Arrays
Declaring Arrays
fixed-size sequential collection of elements of the same type.
type arrayName [ arraySize ];
double balance[10];
Initializing Arrays:
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};.
double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
Accessing Array Elements:
balance[4] = 50.0;
double salary = balance[9];
Code
Multi-dimensional Arrays
type name[size1][size2]...[sizeN];
type arrayName [ x ][ y ];
int threedim[5][10][4];
AccessingTwo-DimensionalArrayElements:
int val = a[2][3];
Initializing Two-Dimensional Arrays:
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Code
Pointers
Why
Some C++ tasks are performed more easily with pointers,
ampersand (&)
denotes an address in memory.
cout << "Address of var1 variable: ";
cout << &var1 << endl;
What Are Pointers?
A pointer is a variable whose value is the address of another variable.
Declaration
type *var-name;
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
Recursion
Def
A recursive function in C++ is a function that calls itself.

C++ course start