Tags: lec2

Q: What are each of these two commands doing?
1. g++ -c hello.cpp
2. g++ hello.o -o hello
A: 1. compiling
2. linking

Q: What are some reasons that having separate compilation and linking steps is useful?
A: Distributed builds: different cpp files could be compiled on different computers (to speed up compile time) -- then they could all be linked together at the end
Incremental builds: if you have a project with lots of cpp files that takes a long time to compile, it's nice if you only need to re-compile one cpp file if that's all your changing between runs

Q: If you want to represent 1,000 in C++, how can you do that?
A: Commas are not allowed in numbers, but you could do 1'000.  Of course, 1000 is also valid.

Q: If you want C++ to automatically determine the type of your variable, what should you do?
A: use "auto" where you would normally put a type name (like "int" or "bool").

Q: How do you print something out?
A: First, include like this:
#include <iostream>
Then, print like this:
std::cout << "hi\n";

Q: What is the type of 5ll?
A: a "long long" integer

Q: if you use the "bool" type, how many bits will it consume, and why?
A: 8 bits, because C++ uses a full byte for the boolean, even though it can be represented as 0 or 1.
This is because memory is "byte addressable", meaning each byte in the address space has a unique address, but individual bits are not given individual addresses.  Sometimes we want to have an address for a bool, so C++ uses a whole byte.

Q: If you have a LOT of booleans and want to store them in a memory efficient way, what might you use?
A: bitset

Q: What is the format for a for-each loop?  Assume you want to loop over "items" and use a variable "curr".
A: for (auto curr : items) {
   ...
}

Q: What are each of these an example of?
void f(); // A
void f() {
    // B
}
A: A: function declaration
B: function definition

Q: What does declaring a function accomplish?
A: It is a way of telling the C++ compiler to trust us (the programmer) that such a function exists.  We want to communicate this to the compiler so that code calling the function compiles even if the compiler hasn't seen the function definition yet.
The function definition might be further down in the cpp file, in another file, or perhaps it may not exist (we gave the compiler incorrect info).  In this last case, the code will compile, but we won't be able to link it in the end.

Q: What is overloading?
A: Two or more functions in the same namespace have the same name, but it's OK because they have parameters of different types.  C++ will try to choose the "best" function to use based on the types of arguments when a call occurs.  If there's not a clearly "best" function to call among the overloads, we could get a length error about the call being "ambiguous".

Q: C++ cannot return multiple values, but in cases where you want to do so, what's a good option?
A: Use the pair type -- you're technically returning a single value, but the pair will contain two values.

Q: How can we create a vector of ints containing 1, 2, 3?
A: vector<int> nums{1,2,3};