|
|
|
Lecture 22 — introduction to C++
|
|
|
|
|
C++ is huge
|
|
|
|
|
can pretty much do everything
|
|
|
|
|
we’ll cover a small set of core concepts
|
|
|
|
|
lots of libraries
|
|
|
|
|
recommended book: C++ Primer Plus, Stephen Prata, Addison-Wesley Professional; 6 edition, 2011
|
|
|
|
|
very thorough (1200 pages)
|
|
|
|
|
compiling
|
|
|
|
|
C++ files typically end in .cc or .cpp or .cxx or .C
|
|
|
|
|
headers are still .h
|
|
|
|
|
use g++ instead of gcc
|
|
|
|
|
g++ -Wall -std=c++11 -o hello hello.cpp
|
|
|
|
|
#include syntax
|
|
|
|
|
C++ standard library headers don’t have .h
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <cstdio> // how you use stdio.h in C++
|
|
|
|
|
local header files included the same way as C
|
|
|
|
|
#include "my_header.h"
|
|
|
|
|
namespaces
|
|
|
|
|
in C, everything needs to have a unique name
|
|
|
|
|
in Java, classes can be grouped inside packages
|
|
|
|
|
only the package needs a unique name
|
|
|
|
|
similar to packages in Java, C++ has namespaces
|
|
|
|
|
namespace funtimes { // code associated with this namespace }
|
|
|
|
|
everything in the standard library is in namespace std
|
|
|
|
|
use :: to specify the namespace
|
|
|
|
|
#include <vector> std::vector<int> first; // empty vector of ints
|
|
|
|
|
:: is called the scope resolution operator
|
|
|
|
|
can avoid having to put std:: everywhere with a using declaration
|
|
|
|
|
#include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; }
|
|
|
|
|
I/O
|
|
|
|
|
standard library provides specific objects for dealing with stdout, stdin, etc.
|
|
|
|
|
#include <iostream>
|
|
|
|
|
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)
|
|
|
|
|
<< is an overloaded operator
|
|
|
|
|
method of the ostream class, takes a value, returns the object instance (i.e., it returns cout)
|
|
|
|
|
std::cin reads from stdin
|
|
|
|
|
int x; std::cin >> x;
|
|
|
|
|
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; }
|
|
|
|
|
pass-by-reference
|
|
|
|
|
syntactic sugar to handle passing around pointers to things
|
|
|
|
|
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)
|
|
|
|
|
void f(int *x) { (*x)++; }
|
|
|
|
|
this fixes it, but now we have to call it like f(&n)
|
|
|
|
|
void f(int &x) { x++; }
|
|
|
|
|
passes in a reference to x
|
|
|
|
|
makes it as if the caller said f(&n) and every reference to x is like (*x)
|
|
|
|
|
const
|
|
|
|
|
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
|
|
|
|
|
const int x = 10;
|
|
|
|
|
x is read-only, will always be 10
|
|
|
|
|
const int *ptr = &n;
|
|
|
|
|
ptr points to a read-only value, *ptr can never be changed
|
|
|
|
|
*ptr = 42; will cause a compiler error
|
|
|
|
|
where ptr points can change
|
|
|
|
|
ptr = &y; is fine
|
|
|
|
|
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
|
|
|
|
|
these can be combined: const int * const ptr = &n;
|
|
|
|
|
neither ptr nor *ptr can be changed
|
|
|
|
|
exercise: will this compile?
|
|
|
|
|
void f(int &x) { x++; }
int main() { const int i = 7; f(i); }
|
|
|
|
|
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
|
|
|
|
|
how about this?
|
|
|
|
|
void f(const int &x) { x++; }
int main() { const int i = 7; f(i); }
|
|
|
|
|
still no, but now the error is error: increment of read-only reference ‘x’ x++; ^
|
|
|
|
|
we can’t modify a const reference
|
|
|
|
|
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
|
|
|
|
|
memory allocation
|
|
|
|
|
C++ has the same stack/heap setup as C
|
|
|
|
|
main difference: use keywords to allocate/deallocate on the heap instead of standard library functions
|
|
|
|
|
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
|
|
|
|
|
delete instead of free
|
|
|
|
|
delete[] arr; // must use delete[] when freeing an array
|
|
|
|
|
delete free_list;
|
|
|