1 of 21

C++ BASICS

2 of 21

OPEN WEBSITE

Go to https://www.online-cpp.com/

3 of 21

REVIEW DEFAULT FILE

  • Comment
  • Include
  • Semi-colons
  • Main plus curly brackets
  • cout
  • return
  • Using namespace

4 of 21

REMOVE USING NAMESPACE

  • Remove using namespace and make it build
  • Run it
  • Duplicate the line and change the text in between the “”
  • Run it

(add std:: to the cout line)

5 of 21

ADD END OF LINE TO EACH LINE

  • Add << std::endl between the last “ and the ; on each cout line
  • Run it

6 of 21

ADD USING STD::ENDL AND STD::COUT

  • Add before int main():
    • using std::cout;
    • using std::endl;
  • Remove the std:: from before cout and endl
  • Run it

7 of 21

VARIABLES

  • int
  • unsigned int
  • double
  • float
  • bool
  • Define a variable of each type and initialize it to any valid value.
  • Print it using cout
  • Run it
  • Try giving it invalid values or removing the semi-colons to see what happens

8 of 21

MATH

  • Addition – Create a variable that is your double plus 10.5
  • Subtraction – Create a variable that is your double minus 3.5
  • Multiplication – Create a variable that is your double times 3.5
  • Division – Create a variable that is your double divided by 2.5
  • Print your new values
  • Run it
  • Do the same things with your floats, ints and unsigned ints
  • What happens if instead of hard coding the value you use constexpr?
  • What happens if you divide by 0?

9 of 21

SPECIAL MATH FUNCTION

  • ++ increment
  • -- decrement
  • Pre vs. Post increment/decrement operations
  • Comment out previous code changes (everything between the first curly bracket and return 0; in main)

10 of 21

SPECIAL MATH FUNCTION

  • Add the following code inside main

int a = 21;

int c;

c = a++;

cout << “==>> a: “ << a << endl;

cout << “==>> c: “ << c << endl;

c = ++a;

cout << “==>> a: “ << a << endl;

cout << “==>> c: “ << c << endl;

11 of 21

FOR LOOPS

  • There are other types of loops, but we’ll focus on this one for now
  • Create

for (auto i=0; i<3; ++i)

{

cout << “value = “ << i << endl;

}

  • Run it

12 of 21

METHODS

  • Comment out previous code changes (everything between the first curly bracket and return 0; in main)
  • Create a method named int PrintFactorial(int value)
  • Create an int variable output and set it equal to 1
  • Create a for loop that loops from 1 to the value passed
  • Inside the loop output *= the loop index
  • Return output
  • Call this method from main and print the value returned
  • Run it

13 of 21

METHODS

#include<iostream>

#include "Factorial.h“

using std::cout;

using std::endl;

int main()

{

constexpr int value = 3;

auto fact = new Factorial();

cout << "The factorial for " << value << " is " << fact->Calc(value) << endl;

return 0;

}

14 of 21

METHODS

#include<iostream>

#include "Factorial.h“

int Factorial::Calc(int value)

{

auto output = 1;

for (auto i=1; i<= value; ++i)

{

output *= I;

}

return output;

}

15 of 21

METHODS

#pragma once

class Factorial

{

Factorial() = default;

~Factorial() = default();

int Calc(int value);

};

16 of 21

METHODS - CONTINUED

  • Switch your factorial code to use a decrement instead of a increment

17 of 21

METHODS – PASS BY VALUE

  • Variable is passed by type
  • Value is copied to the method, so it isn’t altered in the caller

18 of 21

METHODS – PASS BY VALUE

  • Create a method named ScaleValue that takes in two double values and returns void
  • The first parameter should be the value and the second the scale factor
  • Inside the method set the value *= scaleFactor;
  • Print the value and the scale factor inside this method
  • Call it from main and print the values as well.
  • Run it

19 of 21

METHODS – PASS BY REFERENCE

  • Modify the first parameter to be an integer reference (double&)
  • What is different when you run it?

20 of 21

METHODS – PASS BY POINTER

  • Modify the first parameter to be pointer to an integer (int*)
  • Inside the method, you need to dereference the pointer to get to the integer to do the math (*value *= scaleFactor;)
  • What is different when you run it?

21 of 21

METHODS – COMPARISON OF VARIABLE PASSING

  • Pass by value when the value doesn’t change (particularly simple variables)
  • Pass by reference when it needs to change (or const reference if it is a complex variable type when it isn’t changing)
  • Pass by pointer when you need to