1 of 60

Topic 1: Intro to C++

</> by: Aly Soliman

2 of 60

What is C++?

C++ and C# are child languages originated from the C language.

C language Family:

C : lower level, more complex

C++ : updated version

C# : Higher level, simpler to read

3 of 60

Why c++?

GAME DEVELOPMENT

BANKING APPLICATIONS

COMPANY MANAGEMENT SYSTEMS

4 of 60

5 of 60

Important Terms

6 of 60

Syntax:

set of rules that defines what the different combinations of symbols mean.

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4.     cout << "Hello world";
  5. }

7 of 60

Compiler:

  • Translates written code to machine code

8 of 60

What is an IDE? (Integrated Development Environment)

  • software for building applications

9 of 60

Terminal:

  • A terminal is a window where you’ll view and interact with your code from.

10 of 60

File Types:

11 of 60

How to view extensions

s

.1

.2

12 of 60

  • File.pptx PowerPoint document.
  • File.docx Word document
  • File.cpp Files containing C++ code.
  • File.exe executable file.

filename extensions

13 of 60

Installation

14 of 60

Let’s get started :)

15 of 60

Hello world:

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4.     cout << "Hello world“ << endl;
  5. return 0;
  6. }

16 of 60

Basic Syntax:

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. ……
  5.     return 0;
  6. }

This are the basics that will always be typed in any program.

This is the space where you’ll write your code

17 of 60

(Input-output stream)

Basic library used for standard inputting and outputting .

  1. #include <iostream>

18 of 60

2.using namespace std;

Redacts the need to add std:: before every function obtained from the <iostream> library

  1. #include <iostream>
  2. int main(){
  3.     std::cout << "Hello world";
  4. std::cout << “Other Text";
  5. }
  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4.     cout << "Hello world";
  5. cout << “Other Text";
  6. }

19 of 60

  • Entry point of the program. Function from where the program will start to execute.

3.int main(){ …… }

  • libraries are placed out of main function as setup before the main code starts.
  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4.     cout << "Hello world";
  5. return 0;
  6. }

20 of 60

  • cout: (pronounced “see out”) is a function used together with the insertion operator << to output/print text. In this example it will output Hello World.
  • endl: (pronounced “end-el”) short for end line. Tells the program to finish writing in the current line. Used when writing on multiple lines

4.cout << "Hello world“ << endl;

21 of 60

  • causes the main function to finish.
  • A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution.

5.return 0;

22 of 60

23 of 60

How to run your code

24 of 60

Topic 2:�Input/Output

</> by: Aly Soliman

25 of 60

What is a library?

  • package of code reused by many programs.

26 of 60

cout :

  • display the output to the standard output device

cin :

  • display the input to the standard input device

27 of 60

endl vs \n

  • Both endl and \n serve the same purpose in C++ – They insert a new line. However, the key difference between them is that endl causes a flushing of the output buffer every time it is called, whereas \n does not.

#include <iostream>

using namespace std;

int main(){

    cout << "Hello world\n";

    cout << "Hello world" << endl;

}

28 of 60

Variables:

  • int : stores integers, e.g. 123 or -123.
  • double : stores floating point numbers, e.g. 19.99
  • char : stores single characters, e.g. 'a' or 'B'.
  • string : stores text, e.g. "Hello World".
  • bool : stores values with two states: true or false. ‘1’ or ‘0’

  • containers for storing data values.

29 of 60

Arithmetic operators:� ( +, -, *, /, % ,++,--)

  • Addition and subtraction (+ and –)
  • Multiplication and division (* and /)
  • Modulo (%): this gives the remainder of a division e.g. 71 % 9 = 7 as the remainder is 8
  • Increment and decrement (++ and --): increases or decreases a variable by 1.

30 of 60

Write your own code :)

31 of 60

Topic 3:�If statements

</> by: Aly Soliman

32 of 60

If statement:

If the condition between the brackets is true, the functions between curly brackets is executed.

33 of 60

Relational and equality operators

==

Equal to

!=

Not equal to

> , <

Less and Greater than

>=

Greater than or equal to

<=

Less than or equal to

34 of 60

Logical operators:

&& 

Logical and

Returns true if both statements are true

x < 5 &&  x < 10

|| 

Logical or

Returns true if one of the statements is true

x < 5 || x < 4

!

Logical not

Reverse the result, returns false if the result is true

!(x < 5)

35 of 60

Relational VS Arithmetic operators

36 of 60

Else statement:

Use the else statement to specify a block of code to be executed if the condition of the if statement is false.

37 of 60

Write your own code :)

38 of 60

Switch statement:

  • Use the switch statement to select one of many code blocks to be executed.
  • Break exits out of a switch block.

int x = 9;

    switch (x)

    {

    case 2:

        cout << "x equals 2";

        break;

   

    case 3:

        cout << "x equals 3";

        break;

   

    case 9:

        cout << "x equals 9";

        break;

   

    default:

        break;

    }

Switch block

39 of 60

Simpler If & Else statements:

  • Called “Ternary Operators” they consist of a simple true or false question and a first function which is executed if question results in TRUE and executes the second function otherwise.

int x = 10, y = 2, val;

int main(){  

    x > y ? val = x : val = y;

}//val will be equal 10

40 of 60

Comments:

  • // Text : sets all text in same line after it to be ignored as a comment
  • /* Text */ : sets all text between /* and */ to be ignored as a comment

int main(){

    cout << "Hello world"; //This will output Hello world

}

int main(){

    cout << "Hello world"; /*This will output Hello world*/

}

41 of 60

Write your own code :)

42 of 60

Topic 4:�loops

</> by: Aly Soliman

43 of 60

While loops:

If the condition between the brackets is true, the functions between curly brackets is executed repeatedly until the condition is false.

44 of 60

For loops:

When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.

45 of 60

for (int i = 0; i < 20; i++)

  • int i = 0 sets a variable that acts as a counter.
  • i < 20 checks if “i” is less than 20.
  • i++ increments “i” by 1.

  • The first statement only happens once but the other two are checked every time the block of code ends.

46 of 60

Topic 5:�Arrays[]

</> by: Aly Soliman

47 of 60

What are arrays?

Used to store multiple values in a single variable.

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

This is the index, the number of values in an array is entered between square brackets.

The values are placed between curly brackets. Each value is separated from the other by a comma.

48 of 60

49 of 60

Topic #:�Modifying Strings

</> by: Aly Soliman

50 of 60

Library for strings:

  1. #include <string>
  2. using namespace std;
  3. string text = "Hello world";
  4. int main(){  
  5.     std::cout << text;
  6. }

51 of 60

Manipulating strings

52 of 60

Adding strings:

You can join two or more strings together by adding a “+” sign between them.

string firstName = "John ";

string lastName = "Doe";

string fullName = firstName + lastName;

cout << fullName;

53 of 60

C++ Numbers And Strings:

  1. Adding two integers will just be a mathematical equations
  2. Adding two strings will lead to the joining together in the order of first to last
  3. It is not possible to add a string and an integer together

int x = 10;

int y = 20;

int z = x + y; // z will be 30 (a integer)

string x = "10";

string y = "20";

string z = x + y; // z will be 1020 (a string)

string x = "10";

int y = 20;

string z = x + y; //error

54 of 60

.toupper();

It is added after the string name and gets the number of characters in the string.

string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

cout << "The length of the txt string is: " << txt.length();

This will output: “The length of the txt string is: 26

55 of 60

separating strings:

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string text = "Hello world";
  5. int main(){  
  6.     cout << text[0];
  7. }

This will output: “H

56 of 60

.length();

It is added after the string name and gets the number of characters in the string.

string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

cout << "The length of the txt string is: " << txt.length();

This will output: “The length of the txt string is: 26

57 of 60

Break and Continue

58 of 60

Global vs Local variables

  • Global variable: variable declared in the main body of the source code, outside all functions.
  • Local variable: variable declared within the body of a function.

59 of 60

60 of 60

endl �vs �\n

  • Both endl and \n serve the same purpose in C++ – They insert a new line. However, the key difference between them is that endl causes a flushing of the output buffer every time it is called, whereas \n does not.

#include <iostream>

using namespace std;

int main(){

    cout << "Hello world\n";

    cout << "Hello world" << endl;

}