Lecture 02: C++ Basics Continued

Jan 15, 2024  β”‚  Last updated Jan 10, 2024 by Charlotte Curtis

HTML Slides html β”‚ PDF Slides PDF

Where we left off

Predict the data type

5 + i * 2
d + i * 2
d / 9.33
7 / i
7.0 / i
42 + 7 / (i * 1.2)

Today’s topics

Textbook Sections 2.2, 2.5

A few new operators

Like Python, C++ has the compound assignment operators +=, -=, *=, /=, and %=. There’s a few new ones as well:

Constants using const

In Python, constants are just a convention:

PI = 3.14159
GST = 0.05
NUM_PLANETS = 8

In C++, use the keyword const:

const double PI = 3.14159;
const double GST = 0.05;
const int NUM_PLANETS = 8;

C++ has a number of modifiers that make the compiler enforce rules, turning run-time or logic errors into compile time errors (a good thing!)

Comments

Displaying output

Assuming #include <iostream> and using namespace std; we can display output with:

cout << "Hello World!\n";

Unlike Python, we need to explicitly add \n or endl to get a new line

More output magic

Like Python’s print, C++ is happy to mix and match types:

int age = NOT_TELLING;
cout << "I am " << age << " years old.\n";

You can insert as many things in the string as you like, and even break over lines:

cout << "This is a very long string that I want to break over "
     << "multiple lines.\n"
     << "This is on the next line.\n";

Reading input

Like the standard output cout, C++ has a standard input cin:

cin >> variable_name;

The cin input stream

Like cout, cin can be used to read multiple values:

char first_initial, last_initial;
int year, age;
cout << "Enter your first and last initials: ";
cin >> first_initial >> last_initial;

cout << "Enter your program year and current age: ";
cin >> year >> age;

cout << "Thanks, " << first_initial << last_initial
     << "! You were " << (age - year) << " when you started!\n";

emoji Check-in 1/2

True or false:

Like Python, C++ will include a prompt for the user when requesting input.

  1. True
  2. False

emoji Check-in 2/2

True or false:

Multiple inputs can be separated by whitespace.

  1. True
  2. False

Buffered input

Type-dependent input processing

Data TypeInput Processing
intRead all characters until a non-digit is found
doubleRead all characters until a non-digit or non-decimal is found*
charRead the next character

Type casting

Mixed type arithmetic can result in implicit type casting:

int i = 1;
double d = (1 + i) * 3.4; // ok
d = i; // still okay
i = d; // compiler warning!

emoji Type casting check-in

In the following code sample, what is the final value of pi_i?

  1. 0
  2. 1
  3. 2
  4. 3
double pi = 3.14159;
int pi_i = static_cast<int>(pi / 2);

Limitations of double

Limitations of int

Debugging with gdb

gdb demo

Basic gdb commands

CommandDescription
runRun the program
listList the source code
b <line number>Set a breakpoint at the given line number
d <breakpoint number>Delete the given breakpoint
nExecute the next line of code
p <variable name>Print the value of the given variable
cContinue execution until the next breakpoint

Coming up next

Textbook Chapter 4 and start of 5



Previous: Lecture 01: C++ Basics
Next: Lecture 03: Functions