27
Data Structures September 14 – Templates Wade Fagen-Ulmschneider

September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Data Structures

September 14 – TemplatesWade Fagen-Ulmschneider

Page 2: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

#pragma once

#include "Shape.h"

class Square : public Shape {

public:

double getArea() const;

private:

// Nothing!

};

Square.h1

2

3

4

5

6

7

8

9

10

11

Square.cpp8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

class Shape {

public:

Shape();

Shape(double length);

double getLength() const;

private:

double length_;

};

4

5

6

7

8

9

10

11

12

Shape.h

Page 3: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Derived Classes[Public Members of the Base Class]:

[Private Members of the Base Class]:

int main() {

Square sq;

sq.getLength(); // Returns 1, the length init’d

// by Shape’s default ctor

...

}

5

6

7

8

main.cpp

Page 4: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

PolymorphismObject-Orientated Programming (OOP) concept that a single object may take on the type of any of its base types.

Page 5: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Virtual

Page 6: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Cube.cpp12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// No print_1() in RubikCube.cpp

RubikCube::print_2() {

cout << "Rubik" << endl;

}

// No print_3() in RubikCube.cpp

RubikCube::print_4() {

cout << "Rubik" << endl;

}

RubikCube::print_5() {

cout << "Rubik" << endl;

}

RubikCube.cpp12

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

Cube::print_1() {

cout << "Cube" << endl;

}

Cube::print_2() {

cout << "Cube" << endl;

}

virtual Cube::print_3() {

cout << "Cube" << endl;

}

virtual Cube::print_4() {

cout << "Cube" << endl;

}

// In .h file:

virtual Cube::print_5() = 0;

Page 7: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Runtime of Virtual Functions

virtual-main.cpp Cube c; RubikCube c;

RubikCube rc;

Cube &c = rc;

c.print_1();

c.print_2();

c.print_3();

c.print_4();

c.print_5();

Page 8: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Why Polymorphism?

Page 9: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

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

Page 10: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

MP1 Artwork

Page 11: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 12: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 13: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 14: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 15: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 16: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 17: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 18: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout
Page 19: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Abstract Class:[Requirement]:

[Syntax]:

[As a result]:

Page 20: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

class Cube {

public:

~Cube();

};

class RubikCube : public Cube {

public:

~RubikCube();

};

virtual-dtor.cpp15

16

17

18

19

20

21

22

23

Page 21: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

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

Page 22: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Abstract Data Type

Page 23: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

List ADT

Page 24: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

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

Page 25: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

Templates

Page 26: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

T maximum(T a, T b) {

T result;

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

return result;

}

template1.cpp1

2

3

4

5

6

7

Page 27: September 14 Templates - cs225.jtolar.net€¦ · Cube.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 // No print_1() in RubikCube.cpp RubikCube::print_2() {cout

#pragma once

class List {

public:

private:

};

#endif

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