cs 225
play

CS 225 Data Structures Sept. . 17 Templates and Linked Memory ry - PowerPoint PPT Presentation

CS 225 Data Structures Sept. . 17 Templates and Linked Memory ry Wade Fagen-Ulmschneider animalShelter.cpp 5 class AnimalShelter { 6 public: 7 Animal & adopt(); // ... }; animalShelter.cpp 1 class Animal { 2 public: 3


  1. CS 225 Data Structures Sept. . 17 – Templates and Linked Memory ry Wade Fagen-Ulmschneider

  2. animalShelter.cpp 5 class AnimalShelter { 6 public: 7 Animal & adopt(); … // ... };

  3. animalShelter.cpp 1 class Animal { 2 public: 3 void speak() { } 4 }; 5 6 class Dog : public Animal { 7 public: 8 void speak() { } 9 }; 10 11 class Cat : public Animal { 12 public: 13 void speak() { } 14 };

  4. Abstract Class: [Requirement]: [Syntax]: [As a result]:

  5. MP2: cs225/PNG.h 18 class PNG { 19 public: 23 PNG(); 30 PNG(unsigned int width, unsigned int height); 37 PNG(PNG const & other); 43 ~PNG(); 50 PNG & operator= (PNG const & other); 57 bool operator== (PNG const & other) const; 73 bool readFromFile(string const & fileName); 80 bool writeToFile(string const & fileName); 90 HSLAPixel & getPixel(unsigned int x, unsigned int y) const; 96 unsigned int width() const; // ... 118 private: 119 unsigned int width_; 120 unsigned int height_; 121 HSLAPixel *imageData_; 127 void _copy(PNG const & other); 132 };

  6. virtual-dtor.cpp 4 class Cube { 5 public: 6 ~Cube() { std::cout << "~Cube() invoked." << std::endl; } 7 }; 8 9 class RubikCube : public Cube { 10 public: 11 ~RubikCube() { std::cout << "~RubikCube() invoked." << std::endl; } 12 }; 27 std::cout << "Non-virtual dtor:" << std::endl; 28 Cube *ptr = new RubikCube(); 29 delete ptr;

  7. virtual-dtor.cpp 15 class CubeV { 16 public: 17 virtual ~CubeV() { std::cout << "~CubeV() invoked." 18 << std::endl; } 19 }; 20 21 class RubikCubeV : public CubeV { 22 public: 23 ~RubikCubeV() { std::cout << "~RubikCubeV() invoked." 24 << std::endl; } 25 }; 31 std::cout << "Virtual dtor:" << std::endl; 32 CubeV *ptrV = new RubikCubeV(); 33 delete ptrV;

  8. Mattox Monday

  9. Abstract Data Type

  10. List ADT

  11. What types of “stuff” do we want in our list?

  12. Templates

  13. template1.cpp 1 2 3 T maximum(T a, T b) { 4 T result; 5 result = (a > b) ? a : b; 6 return result; 7 }

  14. List.h List.cpp #pragma once 1 1 2 2 3 3 4 class List { 4 5 public: 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 private: 14 15 15 16 16 17 17 18 }; 18 19 19 20 20 21 21 22 22

  15. List Im Implementations 1. 2.

  16. Linked Memory ry Ø 2 2 5 C S

  17. List.h 28 class ListNode { 29 T & data; 30 ListNode * next; 31 ListNode(T & data) : data(data), next(NULL) { } 32 };

  18. Linked Memory ry Ø 2 2 5

  19. Linked Memory ry Ø Ø 2 2 C S 5

  20. List.h List.cpp #pragma once #include "List.h" 1 1 2 2 3 template <class T> 3 template <class T> 4 class List { 4 void List<T>::insertAtFront(const T& t) { 5 public: 5 … /* ... */ 6 28 private: 7 29 class ListNode { 8 30 T & data; 9 31 ListNode * next; 10 32 ListNode(T & data) : 11 data(data), next(NULL) { } 12 33 }; 13 34 14 35 15 36 16 37 17 38 18 39 }; 19 40 20 41 21 22 }

  21. ist insertAtFront Running Tim ime of f Lin inked Lis

  22. List.cpp 80 14 void List<T>::printReverse() 81 const { 82 15 83 16 84 17 85 18 86 19 87 20 88 21 89 22 } 90 91 92 93 94 95 96 97 98 99

  23. Linked Memory ry Ø 2 2 5 C S head

  24. Running Time of Linked List printReverse

  25. List.cpp 24 template <typename T> 25 T List<T>::operator[](unsigned index) { 26 27 28 29 30 31 }

  26. List.cpp 33 ListNode *& List<T>::_index(int index) const { 34 35 36 37 38 39 40 }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend