Lecture 4/8: C++


April 8, 2020

đź“‚Associated files

From Python to C++

CS 106B: Programming Abstractions

Spring 2020, Stanford University Computer Science Department

Lecturers: Chris Gregg and Julie Zelenski

The Stanford Campus


Announcements


Slide 2

A Tour Through C++


Slide 3

Overview of C++

The C++ Logo


Slide 4

Comments

You might be used to the following types of comments in Python:

Single-line comments:

# Python:
# A hash comments out the rest of the line
myVar = 5 # everything past the hash is a comment

Multi-line comments:

# Python:
""" This is a multiline
comment, and I end it with three quotes
"""

C++ also has two types of comments:

Single-line comments:

// C++:
// Two forward slashes comment out the rest of the line
int myVar = 5; // everything past the double-slash is a comment

Multi-line comments:

// C++:
/* This is a multiline
comment, and I end it with an asterisk-slash
*/

Slide 5

#include statements

In Python, you import modules, and the equivalent in C++ is a #include (pound include). There are two different ways to use this statement:

#include <iostream> When using the angle brackets, < and >, this tells the compiler to include libraries that are in pre-defined locations based on the setup for your compiler or IDE (e.g., Qt Creator).

#include "all-examples.h" When using double quotes, this tells the compiler to first search in the same directory as the current file. In this case, the .h extension means header file, which we will discuss soon.


Slide 6

Typed Variables


Slide 7

Standard Output


Slide 8

Blocks: curly braces, while loops


Slide 9

for loops


Slide 10

Scoping


Slide 11

Boolean expressions, boolean operators, and the if statement

Expression   Meaning
a < b   a is less than b
a <= b   a is less than or equal to b
a > b   a is greater than b
a >= b   a is greater than or equal to b
a == b   a is equal to b
a != b   a is not equal to b
Operator
(preferred)
  Alternative   Meaning
a && b   a and b   Both a AND b are true
a || b   a or b   Either a OR b are true
!a   not a   If a is true, returns false, and vice-versa

Example code:

// C++:
int firstNum = 1;
string compareText;

for (int secondNum = 0; secondNum < 3; secondNum++) {
    if (firstNum < secondNum) {
        compareText = "less than";
    } else if (firstNum > secondNum) {
        compareText = "greater than";
    } else {
        compareText = "equal to";
    }
    cout << "firstNum is " << firstNum << ", secondNum is " << secondNum << endl;
    cout << "firstNum is " << compareText << " secondNum" << endl << endl;
}

Output:

firstNum is 1, secondNum is 0
firstNum is greater than secondNum

firstNum is 1, secondNum is 1
firstNum is equal to secondNum

firstNum is 1, secondNum is 2
firstNum is less than secondNum

Example code:

int x = 5;
int y = 7;

if (x == 5 && y == 7) {
    cout << "x equals 5 and y equals 7" << endl;
}

if (x == 5 || y == 5) { // note: cannot combine as if (x || y == 5)
    cout << "x equals 5 or y equals 5" << endl;
}

if (x != 5 && y != 5) {
    cout << "x is not equal to 5 and y is not equal to 5" << endl;
} else if (x != 5 || y != 5) {
    cout << "x is not equal to 5 or y is not equal to 5" << endl;
}

if (!(x == 5 && y == 5)) {
    cout << "not (x is equal to 5 and y is equal to 5) (DeMorgan's Law)" << endl;
}

Output:

x equals 5 and y equals 7
x equals 5 or y equals 5
x is not equal to 5 or y is not equal to 5
not (x is equal to 5 and y is equal to 5) (DeMorgan's Law)

Slide 12

Functions

// C++:
#include <cmath>
#include <iostream>

using namespace std;

int main() {
    double fpNum = 1234; // a floating point number 
    double result = sqrt(fpNum); // set result to be equal to the return value of the sqrt function

    cout << "fpNum: " << fpNum << endl;
    cout << "square root of fpNum: " << result << endl;
    return 0; // the return value for main must be an integer.
              // In main(), a return value of 0 means "everything worked out"
}

Output:

fpNum: 1234
square root of fpNum: 35.1283
int factorial(int number) {
    int result = 1;
    for (int n = number; n > 1; n--) {
        result *= n;
    }
    return result; 
}

Slide 13

Passing values to functions by value or reference

# Python:
def doubleValue(x):
    x *= 2
    return x

if __name__ == "__main__":
    myValue = 5
    doubleResult = doubleValue(myValue)

    print(myValue)
    print(doubleResult)

Output (highlight to reveal):

5 10
// C++:
#include<iostream>
using namespace std;

int doubleValue(int x) {
    x *= 2;
    return x;
}

int main() {
    int myValue = 5;
    int result = doubleValue(myValue);

    cout << myValue << endl;
    cout << result << endl;
}

Output:

5
10
#include<iostream>
using namespace std;

void doubleValueWithRef(int &x) {
    x *= 2;
}

int main() {
    int myValue = 5;
    // int result = doubleValueWithRef(myValue); // Error! the function does not return a value now.
    doubleValue(myValue);

    cout << myValue << endl;
}

Output:

10

Slide 14

Header files

A file with an image of '.h'

Header:

// all-examples.h
#pragma once

int square(int x);
bool even(int value);

C++ file:

// all-examples.cpp
#include <iostream>
#include "all-examples.h"

// we can call the functions before the appear in this file, because they've
// already been defined in the header file and we've #included it.
int main() {
    cout << square(15) << endl;
    if (even(42)) {
        cout << "even" << endl;
    } else {
        cout << "odd" << endl;
    }
}

int square(int x) {
    return x * x;
}

bool even(int v) {
    return v % 2 == 0;
}

Output:

225
even