CS 225
Data Structures
Sept. . 17 – Templates and Linked Memory ry
Wade Fagen-Ulmschneider
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
Data Structures
Wade Fagen-Ulmschneider
class AnimalShelter { public: Animal & adopt(); // ... };
animalShelter.cpp
5 6 7 …
class Animal { public: void speak() { } }; class Dog : public Animal { public: void speak() { } }; class Cat : public Animal { public: void speak() { } };
animalShelter.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[Requirement]: [Syntax]: [As a result]:
class PNG { public: PNG(); PNG(unsigned int width, unsigned int height); PNG(PNG const & other); ~PNG(); PNG & operator= (PNG const & other); bool operator== (PNG const & other) const; bool readFromFile(string const & fileName); bool writeToFile(string const & fileName); HSLAPixel & getPixel(unsigned int x, unsigned int y) const; unsigned int width() const; // ... private: unsigned int width_; unsigned int height_; HSLAPixel *imageData_; void _copy(PNG const & other); };
MP2: cs225/PNG.h
18 19 23 30 37 43 50 57 73 80 90 96 118 119 120 121 127 132
class Cube { public: ~Cube() { std::cout << "~Cube() invoked." << std::endl; } }; class RubikCube : public Cube { public: ~RubikCube() { std::cout << "~RubikCube() invoked." << std::endl; } };
virtual-dtor.cpp
4 5 6 7 8 9 10 11 12 std::cout << "Non-virtual dtor:" << std::endl; Cube *ptr = new RubikCube(); delete ptr; 27 28 29
class CubeV { public: virtual ~CubeV() { std::cout << "~CubeV() invoked." << std::endl; } }; class RubikCubeV : public CubeV { public: ~RubikCubeV() { std::cout << "~RubikCubeV() invoked." << std::endl; } };
virtual-dtor.cpp
15 16 17 18 19 20 21 22 23 24 25 std::cout << "Virtual dtor:" << std::endl; CubeV *ptrV = new RubikCubeV(); delete ptrV; 31 32 33
T maximum(T a, T b) { T result; result = (a > b) ? a : b; return result; }
template1.cpp
1 2 3 4 5 6 7
#pragma once class List { public: private: };
List.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
List.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1. 2.
C S 2 2 5
Ø
class ListNode { T & data; ListNode * next; ListNode(T & data) : data(data), next(NULL) { } };
List.h
28 29 30 31 32
2 2 5
Ø
C S
Ø
2 2 5
Ø
#pragma once template <class T> class List { public: /* ... */ private: class ListNode { T & data; ListNode * next; ListNode(T & data) : data(data), next(NULL) { } }; };
List.h
1 2 3 4 5 … 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include "List.h" template <class T>
void List<T>::insertAtFront(const T& t) {
}
List.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Running Tim ime of f Lin inked Lis ist insertAtFront
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 void List<T>::printReverse() const { }
List.cpp
14 15 16 17 18 19 20 21 22
C S 2 2 5
Ø
head
template <typename T> T List<T>::operator[](unsigned index) { }
List.cpp
24 25 26 27 28 29 30 31
ListNode *& List<T>::_index(int index) const { }
List.cpp
33 34 35 36 37 38 39 40