## Introduction to

# C++

J. Reizenstein,

Y. Lifanov,

J. Mascie-Taylor,

Q. Caudron.

# The Good

- Fast & lightweight
- Very popular
- Free & open source
- Objects & templates
- Huge range of external libraries

# The Bad

- Needs compilation
- Less well documented
- Longer to setup
- Lower level syntax

# Hello World

Our first project

- Windows

1. Open Code::blocks
2. File-> New -> Project
3. Select "console application"
4. Name project and open main.cpp

- Linux

1. Edit a new text file called "main.cpp", and have the terminal open at its directory
2. e.g.

       code main.cpp

   , using Visual Studio Code

# Hello World

Our first project

    #include <iostream>
    int main() {
      std::cout << "Hello, world!\n";
      return 0;
    }

Use F9 in Code::blocks to compile and run, or

    g++ main.cpp -Wall && ./a.out

# Hello World

How it would be in C

    #include <stdio.h>
    int main() {
      printf("Hello World!\n");
      return 0;
    }

# Hello World

Including I/O stream header

    #include <iostream>

Tells compiler to

- Copy declarations

# Hello World

Main function

    int main() {
      /* your code here */
      return 0;
    }

- Executed by the OS
- Outputs an integer
- Zero signals success

# Hello World

Standard output stream

    std::cout << "Hello, world!\n";
    //'\n' is the new line character literal

- Member of standard namespace
- Uses stream insertion operator
- Prints text

# Warm up

Change the message

- Use existing code
- Modify output string
- Compile and run

# Example

C++ compilers ignore white space

       #include   <iostream>
    int main()                 {
      using     namespace std;
      cout  <<
        "Hi, C++!\n";return
      0;

    }

TIP: always keep your code neat

# Example

Indentation and spacing

    #include <iostream>
    int main() {
      using namespace std;
      cout << "Hi, C++!\n";
      return 0;
    }

# Primitives

Basic built-in types

# Primitives

Integers

    //<type> <identifier>;
    int my_integer;

# Primitives

Double precision floating point type

    //<type> <identifier>;
    double my_double;

# Primitives

Other examples

    bool  A; //true or false
    char  B; //character
    float C; //single precision

# Operators

Primitive manipulations

# Operators

Assignment

    int a;     //declare
    a = 1;     //assign a literal
    int b = 2; //declare and assign a literal

# Operators

Arithmetic

    double a = 1.0;     //delare and set
    double b = a + 0.5; //b is 1.5
    double c = a*b;     //c is 1.5
    double d = c/b;     //d is 1

# Standard I/O

Output text to screen

# Standard I/O

Print values

    int x = 1; //declare and assign a value
    std::cout << "x is equal to " << x << " :)\n";

# Standard I/O

Print evaluated expressions

    std::cout << "1 + 1 = " << 1 + 1 << " :)\n";

# Exercise

Modify "Hello World" to

1. Declare integers:

- a = 1234
- b = 5678

2. Print out their:

- Sum
- Difference
- Product
- Quotient

# Solution

    #include <iostream>
    int main() {
      int a = 1234,
          b = 5678;
      std::cout << a + b << "  " << a - b << "  "
                << a*b << "  " << a/b << "  "
                << (double)a/b << "\n";
      return 0;
    }

    6912  -4444  7006652  0  0.21733

# Operators

Integer division

    int a = 2;
    int b = 3;
    double c = a/b;         //c = 0
    double d = (double)a/b; //d = 0.(6)

We'll see why this is useful shortly

# Conditionals

Control program flow

# Conditionals

Simple if statement

    bool condition = true; //or false
    if(condition) {
      std::cout << "condition is true!\n";
    }

# Conditionals

If - else

    if(condition) {
      std::cout << "condition is true!\n";
    } else {
      std::cout << "condition is false!\n";
    }

# Conditionals

If - else if - else

    if(A) {
      std::cout << "A is true!\n";
    } else if(B) {
      std::cout << "A is false but B isn't!\n";
    } else {
      std::cout << "Both A and B are false!\n";
    }

# Operators

Relational

    bool a = 1 > 2;  //false
    bool b = 5 <= 8; //true
    bool c = a == b; //false
    bool d = 8 != 9; //true

# Operators

Logical

    bool a = true;
    bool b = not a;   //false, can also use !
    bool c = a and b; //false, can also use &&
    bool d = a or b;  //true, can also use ||

# Exercise

Multiple of 6 and 9

1. Declare an integer
2. Assign a value 740740734
3. Use an if statement

- To tell if it's a multiple of 6 and 9
- Print a message accordingly

# Solution

Modulo operator

    #include <iostream>
    int main() {
      int x = 740740734;
      if(((x/6)*6 - x == 0) && (x % 9 == 0)) {
        std::cout << x << " is divisible by 6 and 9.\n";
      }
      return 0;
    }

# Loops

Repeat code segments

# Loops

Standard C++ for

    //      init.; cond.; incr.
    for(int i = 0; i < 10; i++) {
      std::cout << i << "\n";
    } //counts up from 0 to 9

# Loops

Breaking

    //      init.; cond.; incr.
    for(int i = 0; true;    ) {
      std::cout << i << "\n";
      if(i++ == 5) {
        break;
      }
    } //counts up from 0 to 5

# Exercise

Finding factors

1. Declare an integer x
2. Set its value to 740740734
3. Use a for loop to

- Find all factors of x between 2 and 9
- Print accordingly

# Solution

    #include <iostream>
    int main() {
      int x = 740740734;
      for(int i = 2; i < 10; i++) {
        if(x % i == 0) {
          std::cout << x << " is divisible by "
                    << i << "\n";
        }
      }
      return 0;
    }

# Standard I/O

Standard input stream

    std::cin

- Member of standard namespace
- Uses stream extraction operator

# Standard I/O

Reading from console

    int x;
    std::cout << "Please enter an integer.\n";
    std::cin >> x;

# Exercise

Guessing game

1. Declare an integer x
2. Set x to any value
3. Use standard I/O to

- Ask player to input a guess
- Read the guess from console
- Tell if guess is higher/lower than x

4. Use a loop to allow 5 tries per game

# Solution

Step 1

    int x = 19, guess;
    for(int i = 0; i < 5; i++) {
      std::cout << "Please enter your guess\n";
      std::cin >> guess;
      /* Step 2 here */
    }
    std::cout << "Out of tries :(\n";

# Solution

Step 2

    if(guess > x) {
      std::cout << "Your guess is too high\n";
    } else if(guess < x) {
      std::cout << "Your guess is too low\n";
    } else {
      std::cout << "Bingo :)\n";
      return 0; //Terminate the whole program
    }

# The End