february 7 inheritance and polymorphism...february 7 –inheritance and polymorphism jing jiang the...

20
Data Structures February 7 – Inheritance and polymorphism Jing Jiang

Upload: others

Post on 26-Feb-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Data Structures

February 7 – Inheritance and polymorphismJing Jiang

Page 2: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

The “Rule of Three”If it is necessary to define any one of these three functions in a class, it will be necessary to define all three of these functions:

1.

2.

3.

Page 3: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Example:

#include "Cube.h"

int main() {

cs225::Cube c(10);

c = c;

return 0;

}

1

2

3

4

5

6

7

assignmentOpSelf.cpp

Page 4: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Example:

#include "Cube.h"

Cube& Cube::operator=(const Cube &other) {

_destroy();

_copy(other);

return *this;

}

1

40

41

42

43

44

45

46

assignmentOpSelf.cpp

Page 5: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Learning objectives

•Understand why inheritance is used

•Compare different ways of using polymorphic functions

•Distinguish the impact of “virtual” on destructor

Page 6: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Learning objectives

•Understand why inheritance is used

•Compare different ways of using polymorphic functions

•Distinguish the impact of “virtual” on destructor

Page 7: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Inheritance

Page 8: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

#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 9: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

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 10: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Learning objectives

•Understand why inheritance is used

•Compare different ways of using polymorphic functions

•Distinguish the impact of “virtual” on destructor

Page 11: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

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

Page 12: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Virtual

Page 13: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

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 print_5() = 0;

Page 14: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

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 15: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Why Polymorphism?

Page 16: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

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 17: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Abstract Class:[Requirement]:

[Syntax]:

[As a result]:

Page 18: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

Learning objectives

•Understand why inheritance is used

•Compare different ways of using polymorphic functions

•Distinguish the impact of “virtual” on destructor

Page 19: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions

class Cube {

public:

~Cube();

};

class RubikCube : public Cube {

public:

~RubikCube();

};

virtual-dtor.cpp15

16

17

18

19

20

21

22

23

Page 20: February 7 Inheritance and polymorphism...February 7 –Inheritance and polymorphism Jing Jiang The “Rule of Three” If it is necessary to define any one of these three functions