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

cs 225
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS 225

Data Structures

Sept. . 17 – Templates and Linked Memory ry

Wade Fagen-Ulmschneider

slide-2
SLIDE 2

class AnimalShelter { public: Animal & adopt(); // ... };

animalShelter.cpp

5 6 7 …

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Abstract Class:

[Requirement]: [Syntax]: [As a result]:

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8
slide-9
SLIDE 9

Mattox Monday

slide-10
SLIDE 10

Abstract Data Type

slide-11
SLIDE 11

List ADT

slide-12
SLIDE 12

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

slide-13
SLIDE 13

Templates

slide-14
SLIDE 14

T maximum(T a, T b) { T result; result = (a > b) ? a : b; return result; }

template1.cpp

1 2 3 4 5 6 7

slide-15
SLIDE 15

#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

slide-16
SLIDE 16

List Im Implementations

1. 2.

slide-17
SLIDE 17

Linked Memory ry

C S 2 2 5

Ø

slide-18
SLIDE 18

class ListNode { T & data; ListNode * next; ListNode(T & data) : data(data), next(NULL) { } };

List.h

28 29 30 31 32

slide-19
SLIDE 19

Linked Memory ry

2 2 5

Ø

slide-20
SLIDE 20

Linked Memory ry

C S

Ø

2 2 5

Ø

slide-21
SLIDE 21

#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

slide-22
SLIDE 22

Running Tim ime of f Lin inked Lis ist insertAtFront

slide-23
SLIDE 23

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

slide-24
SLIDE 24

Linked Memory ry

C S 2 2 5

Ø

head

slide-25
SLIDE 25

Running Time of Linked List printReverse

slide-26
SLIDE 26

template <typename T> T List<T>::operator[](unsigned index) { }

List.cpp

24 25 26 27 28 29 30 31

slide-27
SLIDE 27

ListNode *& List<T>::_index(int index) const { }

List.cpp

33 34 35 36 37 38 39 40