cs 1666
play

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


  1. CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++

  2. First, some praise for C++ "It certainly has its good points. But by and large I think it’s a bad ● language. It does a lot of things half well and it’s just a garbage heap of ideas that are mutually exclusive." Ken Thompson, designer and implementer of Unix ○ "It would make him physically ill to think of programming in C++" ● Donald Knuth, referring to Edsgar Dijkstra ○ "Within C++, there is a much smaller and cleaner language ● struggling to get out" Bjarne Stroustrup ○ Creator of C++ ■ 2

  3. Goals for C++ Be a better C ● ○ Continue to provide low-level performance control ○ Stronger typechecking ○ Not entirely backwards compatible ○ "As Close as Possible to C, but no Closer" ● Support data abstraction ● Support object-oriented programming Support generic programming ● 3

  4. C Family Tree by Bjarne Stroustrup 4

  5. C++ history Initial work on "C with classes" started in 1979 ● Precursor to C++ ○ Renamed to C++ in 1983 ● ● C++ 2.0 released in 1989 ● First C++ standard published by ISO in 1998 C++98 ○ More recent standards: ● C++03 ○ C++11 ○ C++14 ○ C++17 ○ 5

  6. C++ history in this course 6

  7. Re: C++ history in this course 7

  8. With great power comes great responsibility When you code something that will definitely break your computer: Rust: C++: 8

  9. Hello world #include <iostream> int main() { std::cout << "Hello, World!\n"; } 9

  10. The scope resolution operator ● :: ● Has a few use cases, e.g.: ○ Accessing global variables ■ int x = 0; int main() { int x = 0; x = 1; ::x = 2; } 10

  11. Other scope resolution operator uses To define class methods outside of the class ● ● To access static class attributes To sort out ambiguities in multiple inheritance ● ● Accessing members of scoped enums To access identifiers defined within a namespace ● 11

  12. Namespaces ● namespace A { 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

  13. Hello world (again) #include <iostream> int main() { using namespace std; cout << "Hello, World!\n"; } 13

  14. Streams Streams convert typed objects to/from a stream of ● characters (bytes) ● std::cout is the standard output stream (STDOUT) << is the output (or "put to") operator ● Overloaded to operate on a stream and different types ○ Returns a reference to the stream that was output to ○ cout << 1 << "b" << var5; 14

  15. Input streams cin is the standard input stream (STDIN) ● ● >> is the input (or "get from") operator ○ 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

  16. Hello world (one more time) #include <iostream> using std::cout; using std::endl; int main() { cout << "Hello, World!" << endl; } 16

  17. Variable declarations Most of the C types you know and love are available: ● ○ E.g., char, bool, short, int, long, signed, unsigned, float, double, void, etc. ● Has additional syntax for initialization via initializer lists ○ double d1 = 0.1; ○ double d2 {2.3}; ○ int i1 = 4.5; ○ int i2 {6.7}; ● Can use auto type when type is clear from initialization: ○ auto d3 = 8.9; ○ auto i3 {10}; 17

  18. Operators + == - != * < / <= % > ++ >= -- && += || -= ! *= & /= | %= ^ ~ 18

  19. Scope Local ● ● Global Class ● ● Namespace 19

  20. Constants ● const ○ "I promise not to change this value" ○ const int ans = 42; ● constexpr ○ Must be evaluated at compile time ○ constexpr int ans = 42; ○ constexpr int res = sum(x, y); ■ Error, cannot be evaluated at compile time 20

  21. Pointer review ● int x; ● int* y; ● int& z; Error ○ ● int& z = x; ● &x ● *y 21

  22. Range for statement In addition to C-style for loops, C++ offers a range for ● 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

  23. NULL What is the difference between NULL , 0 , and '\0' ? ● ● C++ improves these easily confused points with the introduction of nullptr nullptr is a standin for the null pointer ● There is only one, shared by all pointer types ○ Cannot be converted to an int ○ Can be converted to a bool ○ Given int* p : ■ ● if(*p) is the same as if(*p != 0) if(p) is the same as if(p != nullptr) ● 23

  24. OOP 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

  25. OOP2 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

  26. Inline functions Compiler will try to generate code at each point of call ● instead of using function call instruction Can save on function call overhead! ○ ● Use the inline keyword before return type in function 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 ● Note that compiler may not be able to abide by the request to inline 26

  27. -> vs . Rectangle r(5, 2); Square* s = new Square(4); r.printArea(); s->printArea(); 27

  28. Consider the following: 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

  29. Things to add A default constructor ● ○ MyArray() :arr{nullptr}, sz{0} {} ■ Note this should be invoked as MyArray a; ● What's wrong with MyArray a(); ? A destructor ● ○ ~MyArray() { delete[] arr; } ○ Cleans up memory allocated via new in constructor 29

  30. Copying classes Consider the following: ● ○ MyArray m(10); for (auto i=0; i<10; i++) { m[i] = i; } MyArray n = m; MyArray o; o = m; n.sz = 20; n[2] = 20; o.sz = 30; o[3] = 30; 30

  31. Rule of 5 If your class needs any one of these, it probably needs all 5: ● ○ Destructor ○ Copy constructor ○ Move constructor ○ Copy assignment operator ○ Move assignment operator 31

  32. Copy constructor 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

  33. Copy assignment operator 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

  34. rvalues and lvalues type && creates an rvalue reference ● ● E.g.: ○ 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

  35. Consider the following: A class Test that overloads operator+() ● ● Assume we have valid x and y Test objects ● Test t; t = x + y; We create new Test object as the result of x + y ● ○ But it's an rvalue ● Assignment operator will perform a copy of x + y object ● And then the rvalue x + y object will be destroyed! Better to move instead of copy and destroy ● 35

  36. Move constructor MyArray(MyArray&& a) :arr{a.arr}, sz{a.sz} { a.arr = nullptr; a.sz = 0; } 36

  37. Move assignment operator MyArray& operator=(MyArray&& a) { if (this != &a) { arr = a.arr; sz = a.sz; a.arr = nullptr; a.sz = 0; } return *this; } 37

  38. Templates Should be familiar from Java: ● ○ template <typename T> int compare(const T& v1, const T& v2) { if (v1 < v2) return -1; if (v2 < v1) return 1; return 0; } 38

  39. Class template template<typename T> class MyArray { private: T* arr; int sz; public: // … T& operator[](int i); // … }; 39

  40. Virtual functions Functions that are declared, but not defined ● ○ Prefix the function declaration with virtual ● Pure virtual must be implemented by a derived class ○ Suffix the function declaration with =0 ○ Any class with unimplemented pure virtual functions is considered an abstract class and cannot be instantiated. ● Abstract classes can be used to create interfaces 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

Recommend


More recommend