PowerPoint Presentation · CS 225 Data Structures Sept. 17 –Templates and Linked Memory Wade...

Preview:

Citation preview

CS 225Data Structures

Sept. 17 – Templates and Linked MemoryWade Fagen-Ulmschneider

class AnimalShelter {

public:

Animal & adopt();

// ...

};

animalShelter.cpp5

6

7

class Animal {

public:

void speak() { }

};

class Dog : public Animal {

public:

void speak() { }

};

class Cat : public Animal {

public:

void speak() { }

};

animalShelter.cpp1

2

3

4

5

6

7

8

9

10

11

12

13

14

Abstract Class:[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.h18

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.cpp4

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.cpp15

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

Mattox Monday

Abstract Data Type

List ADT

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

Templates

T maximum(T a, T b) {

T result;

result = (a > b) ? a : b;

return result;

}

template1.cpp1

2

3

4

5

6

7

#pragma once

class List {

public:

private:

};

List.h12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

List.cpp12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

List Implementations1.

2.

Linked Memory

C S 2 2 5Ø

class ListNode {

T & data;

ListNode * next;

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

};

List.h28

29

30

31

32

Linked Memory

2 2 5Ø

Linked Memory

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.h12

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.cpp12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Running Time of Linked List 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.cpp14

15

16

17

18

19

20

21

22

Linked Memory

C S 2 2 5Ø

head

Running Time of Linked List printReverse

template <typename T>

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

}

List.cpp24

25

26

27

28

29

30

31

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

}

List.cpp33

34

35

36

37

38

39

40

Recommended