CS 1666
www.cs.pitt.edu/~nlf4/cs1666/
CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, - - PowerPoint PPT Presentation
CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think its a bad language. It does a lot of things half well and its just a garbage heap
www.cs.pitt.edu/~nlf4/cs1666/
heap of ideas that are mutually exclusive." ○ Ken Thompson, designer and implementer of Unix
○ Donald Knuth, referring to Edsgar Dijkstra
struggling to get out" ○ Bjarne Stroustrup ■ Creator of C++
2
○ Continue to provide low-level performance control ○ Stronger typechecking ○ Not entirely backwards compatible ○ "As Close as Possible to C, but no Closer"
3
4
○ Precursor to C++
○ C++98
○ C++03 ○ C++11 ○ C++14 ○ C++17
5
6
7
When you code something that will definitely break your computer:
8
Rust: C++:
#include <iostream> int main() { std::cout << "Hello, World!\n"; }
9
○ Accessing global variables ■ int x = 0; int main() { int x = 0; x = 1; ::x = 2; }
10
11
int x = 7; } namespace B { int x = 8; } int x = 0; int main() { int x = 0; x = 1; ::x = 2; A::x = 3; B::x = 4; }
12
#include <iostream> int main() { using namespace std; cout << "Hello, World!\n"; }
13
characters (bytes)
○ Overloaded to operate on a stream and different types ○ Returns a reference to the stream that was output to
cout << 1 << "b" << var5;
14
○ Again returns a reference to the original stream to allow for chaining ○ Type of right hand side determines type of value being read ○ Generally considers and whitespace to be a delimiter ■ To read a line with whitespace, use std::getline() ○ Will stop reading a number when first non-numeric character is encountered.
15
#include <iostream> using std::cout; using std::endl; int main() { cout << "Hello, World!" << endl; }
16
○ E.g., char, bool, short, int, long, signed, unsigned, float, double, void, etc.
○ double d1 = 0.1; ○ double d2 {2.3}; ○ int i1 = 4.5; ○ int i2 {6.7};
○ auto d3 = 8.9; ○ auto i3 {10};
17
+
/ % ++
*= /= %=
18
== != < <= > >= && || ! & | ^ ~
19
○ "I promise not to change this value" ○ const int ans = 42;
○ Must be evaluated at compile time ○ constexpr int ans = 42; ○ constexpr int res = sum(x, y); ■ Error, cannot be evaluated at compile time
20
○ Error
21
statement
○ for (auto x : arr) cout << x << endl; ■ Copies each item in array arr into x and executes loop body ○ for (auto& x : arr) cout << x << endl; ■ Creates a reference to each array element and executes loop body
22
introduction of nullptr
○ There is only one, shared by all pointer types ○ Cannot be converted to an int ○ Can be converted to a bool ■ Given int* p:
23
class Rectangle { public: Rectangle(int l, int w) :length{l}, width{w} {} void printArea() { std::cout << area() << std::endl; } private: int length, width; int area() { return length * width; } };
24
class Square { public: Square(int s); void printArea(); private: int side; int area(); }; Square::Square(int s) { side = s; } void Square::printArea() { std::cout << area() << std::endl; } int Square::area() { return side * side; }
25
instead of using function call instruction
○ Can save on function call overhead!
declaration
○ Or define a method within the class definition ■ E.g., like we did in Rectangle ■ Can still use inline keyword on methods not defined in class definition
to inline
26
Rectangle r(5, 2); Square* s = new Square(4); r.printArea(); s->printArea();
27
class MyArray { public: MyArray(int s) :arr{new int[s]}, sz{s} {} int& operator[](int i){ return arr[i]; } int size() { return sz; } private: int* arr; int sz; };
28
○ MyArray() :arr{nullptr}, sz{0} {} ■ Note this should be invoked as MyArray a;
○ ~MyArray() { delete[] arr; } ○ Cleans up memory allocated via new in constructor
29
○ MyArray m(10); for (auto i=0; i<10; i++) { m[i] = i; } MyArray n = m; MyArray o;
n.sz = 20; n[2] = 20;
30
○ Destructor ○ Copy constructor ○ Move constructor ○ Copy assignment operator ○ Move assignment operator
31
MyArray(const MyArray& a) :arr{new int[a.sz]}, sz{a.sz} { for (int i=0; i<sz; i++) arr[i] = a.arr[i]; }
32
MyArray& operator=(const MyArray& a) { int* p = new int[a.sz]; for (int i=0; i<a.sz; i++) p[i] = a.arr[i]; delete[] arr; arr = p; sz = a.sz; return *this; }
33
○ int x = 10; ○ int& r = x; ■ OK ○ int&& rr = x; ■ Error, x is an lvalue ○ int& r2 = x + 5; ■ Error, x + 5 is an rvalue ○ int&& rr2 = x + 5; ■ OK
34
t = x + y;
○ But it's an rvalue
35
MyArray(MyArray&& a) :arr{a.arr}, sz{a.sz} { a.arr = nullptr; a.sz = 0; }
36
MyArray& operator=(MyArray&& a) { if (this != &a) { arr = a.arr; sz = a.sz; a.arr = nullptr; a.sz = 0; } return *this; }
37
○ template <typename T> int compare(const T& v1, const T& v2) { if (v1 < v2) return -1; if (v2 < v1) return 1; return 0; }
38
template<typename T> class MyArray { private: T* arr; int sz; public: // … T& operator[](int i); // … };
39
○ Prefix the function declaration with virtual
○ Suffix the function declaration with =0 ○ Any class with unimplemented pure virtual functions is considered an abstract class and cannot be instantiated.
○ No interface keyword like in Java ○ Just a class with unimplemented pure virtual functions ○ Can implement multiple interfaces due to support for multiple inheritance
40
class Container { public: virtual int& operator[](int) = 0; virtual int size() = 0; virtual ~Container() {} };
41
class MyArr_container : public Container { public: MyArr_container(int s) : m(s) { } ~MyArr_container() {} int& operator[](int i) override { return m[i]; } int size() override { return m.size(); } private: MyArray m; };
42
○ Hence minimal lambda expression is [] { }
○ void if no return statement ○ If body is only a single return statement, return type is the type of that return's expression ○ Otherwise, return type must be specified ■
[] (bool x) { if x return 1; else return 2; }
■
[] (bool x)->int { if x return 1; else return 2; }
43
scope within a lambda expression?
○ [] ■ No local names used in lambda body ○ [&] ■ All local names available by reference ○ [=] ■ All local names available by value ○ [x,&y,&z] ■ x available by value, y and z available by reference ○ [&,x] ■ All available by reference except for x available by value ○ [=,&y,&z] ■ All available by value except for y and z available by reference
44
45
array, you'll probably want to use a vector as your general container type in C++
46
and cat to iterate through the elements of that range ○
Implements at least:
■
Increment operator ++
■
Dereference operator *
first element in the vector
element in the vector
47
48