0% found this document useful (0 votes)
22 views47 pages

C++ Presentation 1

Uploaded by

xamdamuuse512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views47 pages

C++ Presentation 1

Uploaded by

xamdamuuse512
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

COURSE : C++

PROGRAMMING

1
WHAT IS C++ LANGUAGE?

C++ is general purpose programming language began as an


expanded version of C.

The C++ was first invented by Bjarne Stroustrup in 1980


at Bell Laboratories in Murray Hill, New Jersey.

Bjarne Stroustrup initially called the new language "C with


Classes." However, in 1983 the name was changed to C++.

C++ is a middle-level programming language.

C++ is a case -sensitive programming language.


30 December 1950
Denmark
2
What C++ Can Do.

❑ The C++ language can be used for building higher-level


applications
❑ It is mainly used in applications and operating systems.
Like( windows , linux ) and games.

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.

is an individual that writes/creates computer software or applications by using programming


languages.

❑ WHAT IS A PROGRAMMING LANGUAGE?


A programming language is a set of commands, instructions and other syntax use to create a
software program.

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 ?

Machine Language : is only language understood by computers.


Machine Language are Almost impossible for humans use because they consist entirely of
Numbers.
Machine Language consists entirely numbers of 0 s and 1s
Machine Language Example :
Below is an example of machine language (binary) for the text “ Orbit College“
01001111 01110010 01100010 01101001 01110100 00100000 01100011 01101111
01101100 01101100 01100101 01100111 01100101

7
WHAT IS A HIGH-LEVEL LANGUAGE?

A HIGH-LEVEL LANGUAGE (HLL) is a programming language such as C, C++, FORTRAN, or C#


, JAVA that enables a programmer to write programs.
List of some popular higher-level languages
❑ C#
❑ C++
❑ JAVA
❑ PYTHON
❑ PHP

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.

IDE’S FOR C++


Cfree , Code Block , Dev C++, Comondo
Visual Studio C++.

11
C++ BASIC SYNTAX

❑ Let us look at a simple code that would print the words


Hello World.

#include <iostream>
using namespace std;

int main() {

cout <<"Hello World";/< prints


Hello World

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 ;

return 0 ends the main function.

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.

// this is a comment on one line


/* this comment is on a different line */

/* this comment runs across


three lines of code just to show
that it can be done ! */
14
C++ VARIABLES

❑ 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

As explained in the variables Lesson, a variable in


C++ must be a specified data type:

DATATYPE NAME = VALUE;

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 4 bytes Stores whole numbers, without decimals


float 4 bytes Stores fractional numbers, containing one or more
decimals.
double 8 bytes Stores fractional numbers, containing one or more
decimals.
boolean 1 byte Stores true or false values

char 1 byte Stores a single character/letter/number

cout <<sizeof(int )<< endl; 17


Variables declaration in C++

❑ Creating a variable reserves a memory location, or a space


in memory for storing values.
❑ The compiler requires that you provide a data type for each
variable you declare.
For example:
Define a variable called Orbit that can hold integer values as follows:

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

Arithmetic Assignment Comparison Logical

21
ARITHMETIC OPERATORS
Arithmetic operators are used to perform common mathematical operations.

Operator Name Description Example

+ Addition Adds together two values x+y

- Subtraction Subtracts one value from another x-y

* Multiplication Multiplies two values x*y

/ Division Divides one value by another x/y

% Modulus Returns the division remainder x%y

++ Increment Increases the value of a variable by 1 ++x

-- Decrement Decreases the value of a variable by 1 --x

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

Comparison operators are used to compare two values.

Note: The return value of a comparison is either true (1) or false (0).

In the following example, we use the greater than operator (>)


to find out if 5 is greater than 3:

Example
int x = 5;
int y = 3;
cout << (x > y); // returns 1 (true) because 5 is
greater than 3

24
COMPARISON OPERATORS

A list of all comparison operators:

Operator Name Example

== Equal to x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y

25
LOGICAL OPERATORS
Logical operators are used to determine the logic between variables
or values:

Operator Name Description Example


&& Logical and Returns true if both statements are x < 5 && x < 10
true
|| Logical or Returns true if one of the statements x < 5 || x < 4
is true
! Logical not Reverse the result, returns false if !(x < 5 && x < 10)
the result is true

26
C++ Conditions statements
C++ Conditions and If Statements
C++ supports the usual logical conditions from mathematics:

Less than: a < b


Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b

You can use these conditions to perform different


actions for different decisions.

27
C++ has the following conditional statements

❑ Use if to specify a block of code to be executed,


if a specified condition is true
❑ Use else to specify a block of code to be executed,
if the some condition is false
❑ Use else if to specify a new condition to test,
if the first condition is false
❑ Use switch to specify many alternative blocks of code to be executed
The if Statement
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.
28
Cont..
❑ In the example below, we test two values to find out if 20 is greater
than 18. If the condition is true, print some text:

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

❑ Use the else statement to specify a block of code to be executed if the


condition is false.

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

❑ Use the else if statement to specify a new condition if the first


condition is false.

Example Example explained


int time = 22; In the example above, time (22) is greater than 10, so
if (time < 10) { the first condition is false.
cout << "Good morning."; The next condition, in the else if statement,
} else if (time < 20) { is also false, so we move on to the else condition
cout << "Good day."; since condition1 and condition2 is both false - and
} else { print to the screen "Good evening".
cout << "Good evening.";
} However, if the time was 14, our program would print
// Outputs "Good evening." "Good day."

31
C++ Switch Statements

❑ Use the switch statement to select one of many code blocks to be


executed.

Syntax This is how it works:


switch(expression) { The switch expression is evaluated once
case x:
// code block The value of the expression is compared
break; with the values of each case
case y:
// code block If there is a match, the associated block of code is
break; executed
default:
// code block The break and default keywords are optional.
}
32
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

❑ Arrays are used to store multiple values in a single variable, instead of


declaring separate variables for each value.
❑ To declare an array, define the variable type, specify the name of the array
followed by square brackets[] and specify the number of elements
it should store:

string students[4];

We have now declared a variable that holds an array of four strings.


To insert values to it, we can use an array literal - place the values in a comma-separated
list, inside curly braces:
Example
string students[4] = {“axmed", “cali", “Xamda", “Nimco"};
cout << students[0];
// Outputs axmed
34
C++ Loops

❑ loop is a sequence of instructions that is continually repeated


until a certain condition is reached.

C++ Loops

While loop Do/ While loop For loop

35
C++ While Loops
❑ Syntax
while (condition) {
// code block to be executed
}

In the example below, the code in the loop will run,


over and over again,
as long as a variable (i) is less than 5:

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
}

The example below will print the numbers 0 to 4:

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"};’

for (int i = 0; i < 4; i++) {


cout << students[i] << "\n"; The following example outputs the index of each element
} together with its value:
Example

string students[4] = {“axmed", “Cali", “Xamda", “Nimco"};


for (int i = 0; i < 4; i++) {
cout << i << ": " << students[i] << "\n";
} 39
C++ Functions

❑ A function is a block of code that performs specific task ().


❑ Functions are used to perform certain actions,
❑ and they are important for reusing code:
Define the code once, and use it many times.

Types of Functions

Standard library function User Defined function

Data type
Function name
Parameters (optional)
40
Create a Function

❑ C++ provides some pre-defined functions, such as main(), which is used to


execute code. But you can also create your own functions to perform certain
actions.
❑ To create (often referred to as declare) a function,
specify the name of the function, followed by parentheses ().
Syntax
void myFunction() {
// code to be executed
}
Example Explained
myFunction() is the name of the function
void means that the function does not have a return value.
inside the function (the body), add code that defines what
the function should do.
41
Call a Function

❑ Declared functions are not executed immediately.


❑ They are "saved for later use", and will be executed later, when they are called.
❑ To call a function, write the function's name followed by two parentheses () and
a semicolon ;
❑ In the following example, myFunction() is used to print a text (the action), when
it is called:
Example
void myFunction() { //header function
cout << "I just got executed!"; //body function
}
int main() {
myFunction(); // call the function
return 0;
}
// Outputs "I just got executed!"
42
Function Declaration and
Definition
❑ A C++ function consist of two parts:
❑ Declaration: the return type, the name of the function, and parameters (if any)
❑ Definition: the body of the function (code to be executed)

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;

void myFunction(string fname, int age) {


cout << fname << " jama. " << age << " years old. \n";
}

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

You might also like