V Lecture 22 — introduction to C++
V C++ is huge
* can pretty much do everything
* we’ll cover a small set of core concepts
* lots of libraries
V recommended book: C++ Primer Plus, Stephen Prata, Addison-Wesley Professional; 6 edition, 2011
* very thorough (1200 pages)
V compiling
* C++ files typically end in .cc or .cpp or .cxx or .C
* headers are still .h
V use g++ instead of gcc
* g++ -Wall -std=c++11 -o hello hello.cpp
V #include syntax
V C++ standard library headers don’t have .h
* #include <iostream>
* #include <cstdio> // how you use stdio.h in C++
V local header files included the same way as C
* #include "my_header.h"
V namespaces
* in C, everything needs to have a unique name
V in Java, classes can be grouped inside packages
* only the package needs a unique name
V similar to packages in Java, C++ has namespaces
* namespace funtimes {
// code associated with this namespace
}
* everything in the standard library is in namespace std
V use :: to specify the namespace
* #include <vector>
std::vector<int> first; // empty vector of ints
* :: is called the scope resolution operator
V can avoid having to put std:: everywhere with a using declaration
* #include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
V I/O
* standard library provides specific objects for dealing with stdout, stdin, etc.
* #include <iostream>
V std::cout << “a message for stdout” << std::endl;
* std::cout is an instance of the ostream class
* std::endl is a system-dependent newline (it’s ‘\n’ on Linux)
V << is an overloaded operator
* method of the ostream class, takes a value, returns the object instance (i.e., it returns cout)
V std::cin reads from stdin
* int x;
std::cin >> x;
V exercise: write a program that asks the user what year they were born, and then prints their age in leap years
* #include <iostream>
using namespace std;
int main() {
int year_born;
cout << "what year were you born? " << ends;
cin >> year_born;
cout << "you are " << (2016 - year_born) / 4 << " leap years old!" << endl;
}
V pass-by-reference
* syntactic sugar to handle passing around pointers to things
V void f(int x) {
x++;
}
* you know this function accomplishes nothing, because x is passed by value (a copy of x is passed in, so the modification only changes a local variable)
V void f(int *x) {
(*x)++;
}
* this fixes it, but now we have to call it like f(&n)
V void f(int &x) {
x++;
}
V passes in a reference to x
* makes it as if the caller said f(&n) and every reference to x is like (*x)
V const
V const is a way of giving a variable properties that will be checked at compile-time
* means “this will not be changed” in different ways
V const int x = 10;
* x is read-only, will always be 10
V const int *ptr = &n;
* ptr points to a read-only value, *ptr can never be changed
* *ptr = 42; will cause a compiler error
V where ptr points can change
* ptr = &y; is fine
V int * const ptr = &n;
* ptr itself is a read-only value, will always point to &n
* *ptr = 42; is fine
* ptr = &y; will cause a compiler error
V these can be combined: const int * const ptr = &n;
* neither ptr nor *ptr can be changed
V exercise: will this compile?
* void f(int &x) {
x++;
}

int main() {
const int i = 7;
f(i);
}
V no, the compiler will say
error: binding ‘const int’ to reference of type ‘int&’ discards qualifiers
f(i);
^
* we can’t pass a const value as a non-const reference
V how about this?
* void f(const int &x) {
x++;
}

int main() {
const int i = 7;
f(i);
}
V still no, but now the error is
error: increment of read-only reference ‘x’
x++;
^
* we can’t modify a const reference
V lastly:
* void f(int x) {
x++;
}

int main() {
const int i = 7;
f(i);
}
* compiles just fine, making a copy of a const value is ok
V memory allocation
* C++ has the same stack/heap setup as C
V main difference: use keywords to allocate/deallocate on the heap instead of standard library functions
V new instead of malloc
* int *arr = new int[10] instead of int *arr = malloc(10*sizeof(*arr))
* Block *free_list = new Block(); // uses constructor for Block class — more on that next lecture
V delete instead of free
* delete[] arr; // must use delete[] when freeing an array
* delete free_list;