C++ Presentation 1
C++ Presentation 1
PROGRAMMING
1
WHAT IS C++ LANGUAGE?
3
The Difference Between Program, Programming,
Programmer And Programming Language.
❑ WHAT IS A PROGRAM?
A computer program is a sequence or set of instructions
in a programming language for a computer to execute.
❑ WHAT IS PROGRAMMING?
computer programming (often shortened to programming or coding) is the
process of designing , writing, testing debugging and maintaining the source
code of computer programs.
4
The Difference Between Program, Programming,
Programmer And Programming Language.
❑ WHAT IS A PROGRAMER?
A programmer, developer ,dev ,coder, or software engineer is a person who creates computer
software or computer programs.
Languages that programmers use to write code are called "High-level Languages".
This code can be compiled into a "Low-Level Language" which is recognized directly by the
computer hardware.
5
LEVELS OF PROGRAMMING LANGUAGE
6
WHAT IS A MACHINE LANGUAGE ?
7
WHAT IS A HIGH-LEVEL LANGUAGE?
8
TRANSLATORS : COMPILER , INTREPETER
❑ What is a Compiler?
A compiler is a computer program which helps you translate source code written in a high-
level language(C++,C# …) into low-level machine language.
❑ An interpreter is a computer program that is used to directly execute program
instructions written using one of the many high-level programming languages.
❑ The interpreter transforms the high-level program into an intermediate language that it
then executes, or it could parse the high-level source code and then performs the
commands directly, which is done line by line or statement by statement.
9
TRANSLATORS : COMPILER , INTREPETER
10
WHAT IS THE IDE ?
integrated development environment (IDE) a software
application that helps programmers develop software
code efficiently.
11
C++ BASIC SYNTAX
#include <iostream>
using namespace std;
int main() {
return 0;
Let us look various parts of this program:
}
12
EXAMPLE EXPLAINED
<iostream> :- is a header file library which contain
pre-defined fuctions(input & output).
using namespace std means that we can use names
for objects and variables from the standard library.
int main() This is called a function. Any code inside its curly
brackets {} will be executed.
cout << "Hello World"; is an object used together with the insertion
operator (<<) to output/print text.
Every C++ statement ends with a semicolon ;
13
COMMENTS IN C++
❑ Comments can be used to explain C++ code, and to make it more readable.
❑ It can also be used to prevent execution when testing code.
❑ Comments can be singled-lined or multi-lined.
❑ Single-line comments start with two forward slashes (//).
❑ This example uses a single-line and multi-lined comments.
❑ In C++ variable is used to store data in a memory location, which can be modified or
used in the program during program execution.
❑ Variables are tools that help the programmer temporarily store data for a finite
amount of time.
❑ In C++, there are different data types of variables (defined with different keywords),
for example:
❑ int - stores integers (whole numbers), without decimals, such as 123 or -123
❑ double - stores floating point numbers, with decimals, such as 19.99 or -19.99
❑ char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single
quotes
❑ string - stores text, such as "Hello World". String values are surrounded by double
quotes
❑ boolean - stores values with two states: true or false 15
C++ DATATYPES
Example
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
16
BASIC DATA TYPES
The data type specifies the size and type of information
the variable will store:
Data Type Size Description
int Orbit=10; Now, let's assign a value to the variable and print it.
#include<iostream> Output:
using namespace std;
int main() {
int var=10; 10
cout<<Orbit;
return 0;
Note: The C++ programming language is case-sensitive,
} so Orbit, orbit and ORBIT are different identifiers.
18
WRITE A PROGRAM IN C++ TO ADDITION
OF TWO NUMBER.
#include<iostream>
using namespace std;
int main() {
Output:
int a,b,sum; Enter the 1st number
cout<<"Enter the 1st number\n"; 10
cin><a;
/<extraction Enter the 2nd number
cout<<"Enter the 2nd number\n"; 20
cin><b; Sum of two number 30
sum=a+b;
/<insertion
cout<<"Sum of two number "<<sum;
return 0;
}
19
C++ OPERATORS
Operator: A symbol or function denoting an operation ( e.g: +,-,/ ).
Operator: A symbol which tells the computer to perform certain
mathematical and logical calculation is know as operator.
+,-,*,/,MOD, OR, NOT are some examples of operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values
Example
int x = 100 + 50;
Although the + operator is often used to add together two values, like in the example
above, it can also be used to add together a variable and a value, or a variable and
another variable:
Example
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400) 20
TYPES OF OPERATORS
21
ARITHMETIC OPERATORS
Arithmetic operators are used to perform common mathematical operations.
22
Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=) to assign
the value 10 to a variable called x:
Example
int x = 10;
A list of all assignment operators:
Operator Example Same As
= Equal x=5 x=5
+= addition x += 3 x=x+3
-= subtraction x -= 3 x=x-3
*= multiplication x *= 3 x=x*3
/= division x /= 3 x=x/3
%= modulo x %= 3 x=x%3
23
COMPARISON OPERATORS
Note: The return value of a comparison is either true (1) or false (0).
Example
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is
greater than 3
24
COMPARISON OPERATORS
== Equal to x == y
!= Not equal x != y
25
LOGICAL OPERATORS
Logical operators are used to determine the logic between variables
or values:
26
C++ Conditions statements
C++ Conditions and If Statements
C++ supports the usual logical conditions from mathematics:
27
C++ has the following conditional statements
Example
if (20 > 18) {
cout << "20 is greater than 18"; Example explained
} In the example above we use two variables, x and y,
to test whether x is greater than y (using
the > operator).
We can also test variables: This Example As x is 20, and y is 18, and we know that
int x = 20; 20 is greater than 18, we print to the screen that "x
int y = 18; is greater than y".
if (x > y) {
cout << "x is greater than y";
}
29
The C++ else Statement
Example explained
Example
In the example above, time (20) is greater than 18,
int time = 20;
so the condition is false.
if (time < 18) {
cout << "Good day.";
Because of this, we move on to the else condition
} else {
and print to the screen "Good evening".
cout << "Good evening.";
}
If the time was less than 18, the program would print
// Outputs "Good evening."
"Good day".
30
The C++ else if Statement
31
C++ Switch Statements
❑ Example of Switch
Example
int day = 4;
switch (day) {
case 6:
cout << "Today is Saturday"; Note: The default keyword must be
break; used as the last statement in the switch,
case 7: and it does not need a break.
cout << "Today is Sunday";
break;
default:
cout << "Looking forward to the Weekend";
}
// Outputs "Looking forward to the Weekend"
33
C++ Arrays
string students[4];
C++ Loops
35
C++ While Loops
❑ Syntax
while (condition) {
// code block to be executed
}
Example
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
} Note: Do not forget to increase the variable used in
the condition, otherwise the loop will never end!
36
C++ Do/While Loops
❑ Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop.
The loop will always be executed
at least once, even if the condition is false,
because the code block is executed
before the condition is tested:
Example
int i = 0;
do {
cout << i << "\n";
i++; Note: Do not forget to increase the variable used in
} the condition, otherwise the loop will never end!
while (i < 5); 37
C++ For Loops
❑ Syntax
for (start point; end point; driver) {
// code block to be executed
}
Example
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}
38
C++ Arrays and Loops
❑ You can loop through the array elements with the for loop.
❑ The following example outputs all elements in the cars array:
Example
string students[4] = {“axmed", “Cali", “Xamda", “Nimco"};’
Types of Functions
Data type
Function name
Parameters (optional)
40
Create a Function
Example
void myFunction(); //declaration
int main() {
myFunction(){
cout << "I just got executed!"; //definition
};
return 0;
}
// Outputs "I just got executed!"
43
Function Parameters and
Arguments
❑ Information can be passed to functions as a parameter. Parameters act as variables
inside the function.
❑ Parameters are specified after the function name, inside the parentheses
❑ You can add as many parameters as you want, just separate them with a comma:
Example
void myFunction(string firstname) {
cout << firstname << " jama\n";
}
int main() {
myFunction(“cali");
myFunction(“guuleed");
myFunction(“axmed");
return 0;
}
//Output
// cali jama
// guuleed jama
// axmed jama 44
Multiple Parameters
❑ Inside the function, you can add as many parameters as you want:
❑ You can add as many parameters as you want, just separate them with a comma:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
myFunction(“faarax", 3);
myFunction(“ciise", 14);
myFunction(“cali", 30);
return 0;
}
45
Return values
❑ The void keyword, used in the previous examples, indicates that the function should not
return a value .
❑ If you want the function to return a value, you can use a data type (such as int, string,
etc.) instead of void, and use the return keyword inside the function:
Example
int myFunction(int x) {
return 5 + x;
}
int main() {
cout << myFunction(3);
return 0;
}
46
Return values
Example 2
int myFunction(int x, int y) {
return x + y;
}
int main() {
cout << myFunction(5, 3);
return 0;
}
// Outputs 8
47