tdde18 726g77
play

TDDE18 & 726G77 Classes & Pointers Christoffer Holm - PowerPoint PPT Presentation

TDDE18 & 726G77 Classes & Pointers Christoffer Holm Department of Computer and informaon science 1 Classes 2 Pointers 3 List lab 4 Special Member Funcons 2 / 82 Classes What is a class? struct Date { int day; int month;


  1. 25 / 82 Classes const member func�ons ‚ If an object is declared as const then no data members can be modified. ‚ This means that only member func�ons that are declared as const are allowed to be called from these objects, with the excep�on of constructors and the destructor.

  2. 26 / 82 Classes Inner class class Outer void Outer::fun() { { public: // ... } void fun(); void Outer::Inner::fun() { // ... class Inner } { public: Outer o{}; void fun(); o.fun(); }; Outer::Inner i{}; // works! i.fun(); };

  3. 26 / 82 Classes Inner class class Outer void Outer::fun() { { public: // ... } void fun(); void Outer::Inner::fun() private: { // ... class Inner } { public: Outer o{}; void fun(); o.fun(); }; Outer::Inner i{}; // doesn't work i.fun(); };

  4. 27 / 82 Classes Inner class ‚ It is possible to declare classes inside other classes. ‚ These classes adhere to the access level they are placed in. ‚ Meaning an inner class declared as private is not accessible from the outside. ‚ To define member func�ons of an inner class you first have to access the outer class, and then the inner class. ‚ So you write Outer::Inner:: before the func�on name.

  5. 28 / 82 Classes friend class Date { // ... private: int day; int month; int year; friend bool same_month(Date d1, Date d2); }; bool same_month(Date d1, Date d2) { return d1.year == d2.year && d1.month == d2.month; }

  6. 29 / 82 Classes friend ‚ It is possible to declare a func�on as a friend to your class. ‚ This allows that friend to access the private members. ‚ Should only be used if absolutely necessary, since it couples the class with a func�on that is not a member.

  7. 1 Classes 2 Pointers 3 List lab 4 Special Member Func�ons

  8. 31 / 82 Pointers What is a pointer? int x{5}; int y{7};

  9. 31 / 82 Pointers What is a pointer? int x{5}; x 5 int y{7}; y 7

  10. 31 / 82 Pointers What is a pointer? int x{5}; x 5 int y{7}; int* ptr{}; y 7

  11. 31 / 82 Pointers What is a pointer? int x{5}; ptr x 5 int y{7}; int* ptr{}; y 7

  12. 31 / 82 Pointers What is a pointer? int x{5}; ptr x 5 int y{7}; int* ptr{&x}; y 7

  13. 31 / 82 Pointers What is a pointer? int x{5}; ptr x 5 int y{7}; int* ptr{&x}; y 7

  14. 31 / 82 Pointers What is a pointer? int x{5}; ptr x 5 int y{7}; int* ptr{&x}; y ptr = &y; 7

  15. 31 / 82 Pointers What is a pointer? int x{5}; ptr x 5 int y{7}; int* ptr{&x}; y ptr = &y; 7

  16. 32 / 82 Pointers What is a pointer? ‚ A pointer is a variable that stores an address to some loca�on in memory. ‚ A pointer can only point to a specific data type (specified by the programmer). ‚ This means that when the pointer retrieves the value stored at the address, it will always be the specified type. ‚ Read pointer declara�ons backwards: int* x reads: x is a pointer( * ) to an int .

  17. 33 / 82 Pointers What is a pointer? ‚ Each variable has a loca�on in memory (they have an address) which can be retrieved with operator& , as such: &x . ‚ A pointer can therefore work like a reference, by storing a specific variables address in the pointer. ‚ The difference between a reference and a pointer is that pointers can change what they point to while references cannot.

  18. 34 / 82 Pointers How to use a pointer int x{5}; int *ptr{&x}; ptr x 5 cout << *ptr << endl;

  19. 34 / 82 Pointers How to use a pointer int x{5}; int *ptr{&x}; ptr x 5 cout << *ptr << endl;

  20. 34 / 82 Pointers How to use a pointer int x{5}; int *ptr{&x}; ptr x 5 cout << *ptr << endl;

  21. 35 / 82 Pointers How to use a pointer ‚ To access the data on the other end of the pointer (i.e. the thing it points to) we have to dereference the pointer. ‚ You dereference a pointer with operator* . ‚ Like this: *ptr .

  22. 36 / 82 Pointers Poin�ng to nothing int* ptr{}; ptr == nullptr;

  23. 37 / 82 Pointers Poin�ng to nothing ‚ The default value of pointers is a special value called nullptr . ‚ A pointer that is nullptr points at nothing. ‚ This is also a big difference between pointers and references; a reference must refer to something.

  24. 38 / 82 Pointers Manual memory int* ptr{};

  25. 38 / 82 Pointers Manual memory int* ptr{}; ptr

  26. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; ptr

  27. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; ptr 5

  28. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; ptr 5

  29. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; *ptr = 7; ptr 5

  30. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; *ptr = 7; ptr 7

  31. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; *ptr = 7; ptr 7 delete ptr;

  32. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; *ptr = 7; ptr delete ptr;

  33. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; *ptr = 7; ptr delete ptr; ptr = nullptr;

  34. 38 / 82 Pointers Manual memory int* ptr{new int{5}}; *ptr = 7; ptr delete ptr; ptr = nullptr;

  35. 39 / 82 Pointers Manual memory ‚ A pointer doesn’t have to point to a variable. ‚ You can place data inside the memory directly with new . ‚ This is called alloca�ng data. ‚ When alloca�ng memory it is your responsibility to destroy the object that lies in that memory. ‚ You can deallocate the memory by calling delete on the pointer.

  36. 40 / 82 Pointers Manual memory ‚ You can only deallocate memory that has been allocated (i.e. for every delete there must be a new ). ‚ You can only delete memory once. ‚ Calling delete does not remove the pointer! ‚ Meaning the pointer will s�ll point to the memory address, but the object has been removed, so it points to nonsense.

  37. 1 Classes 2 Pointers 3 List lab 4 Special Member Func�ons

  38. 42 / 82 List lab ‚ In lab 3 you are going to create a linked list . ‚ This is a list that consists of values that the user inserts. ‚ Each value is stored in a node which then points to the next value in the list. ‚ On the next slide is a simple implementa�on of a node .

  39. 43 / 82 List lab Node struct Node { int value; Node* next; }; Node n2 {2, nullptr}; Node n1 {8, &n2}; Node* first {&n1};

  40. 43 / 82 List lab Node struct Node { int value; first Node* next; }; value value 8 2 Node n2 {2, nullptr}; next next Node n1 {8, &n2}; Node* first {&n1};

  41. 43 / 82 List lab Node struct Node { int value; Node* next; }; Node n2 {2, nullptr}; 8 2 first Node n1 {8, &n2}; Node* first {&n1};

  42. 44 / 82 List lab Node ‚ first points to whichever element is first in the list. ‚ Each Node then points to the next element in the list. ‚ Once the next pointer is nullptr we have reached the end of the list.

  43. 45 / 82 List lab Accessing data in Node Node n2 {2, nullptr}; Node n1 {8, &n2}; Node* first {&n1}; cout << (*first).value << endl;

  44. 45 / 82 List lab Accessing data in Node Node n2 {2, nullptr}; Node n1 {8, &n2}; Node* first {&n1}; cout << first->value << endl;

  45. 46 / 82 List lab Accessing data in Node ‚ You access data members in an object that a pointer points to by either: ‚ Dereferencing the pointer to get normal access ( (*first).value ), or ‚ Using the -> operator ( first->value ). ‚ These two ways are exactly the same. ‚ It is recommended to use -> , since it is easier on the eyes.

  46. 47 / 82 List lab List class List { public: // ... private: Node* first{}; };

  47. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; };

  48. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; };

  49. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; tmp };

  50. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; tmp };

  51. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; tmp };

  52. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 4 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; };

  53. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 4 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; tmp };

  54. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 4 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; tmp };

  55. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 4 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; tmp };

  56. 48 / 82 List lab insert class List { public: void remove(); void insert(int value) first 9 4 { Node* tmp{new Node{value}}; tmp->next = first; first = tmp; } private: Node* first{}; };

  57. 49 / 82 List lab insert ‚ We have to allocate a new Node at inser�on, why? ‚ If tmp is a normal variable, then it will disappear once the insert func�on has finished. ‚ But we want it to persist un�l we want to remove it. ‚ Therefore we have to allocate nodes with new .

  58. 50 / 82 List lab A problem! class List { public: void remove() { first 9 4 first = first->next; } void insert(int value); private: Node* first{}; };

  59. 50 / 82 List lab A problem! class List { public: void remove() { first 9 4 first = first->next; } void insert(int value); private: Node* first{}; Remove };

  60. 50 / 82 List lab A problem! class List { public: void remove() { first 9 4 first = first->next; } void insert(int value); private: Node* first{}; };

  61. 50 / 82 List lab A problem! class List { public: void remove() { first 9 4 first = first->next; } void insert(int value); private: Node* first{}; };

  62. 50 / 82 List lab A problem! class List { public: void remove() { first 9 4 first = first->next; } void insert(int value); Memory leak private: Node* first{}; };

  63. 51 / 82 List lab A problem! ‚ When removing a node it does not disappear by itself. ‚ Since we called new to create it, we have to call delete on the node for it to actually disappear. ‚ The pointer that kept track of our memory got lost, meaning there is no way for us to access it again. ‚ If this is done repeatedly by our program our memory will slowly be filled up by these inaccessible objects. ‚ This type of problem is called memory leak .

  64. 52 / 82 List lab Let’s try again! class List { public: void remove() { first 9 4 Node* tmp = first; first = first->next; delete tmp; } void insert(int value); private: Node* first{}; };

Recommend


More recommend