Computer Science II for Majors Lecture 15 Polymorphism (Continued) - - PowerPoint PPT Presentation

computer science ii for majors
SMART_READER_LITE
LIVE PREVIEW

Computer Science II for Majors Lecture 15 Polymorphism (Continued) - - PowerPoint PPT Presentation

CMSC202 Computer Science II for Majors Lecture 15 Polymorphism (Continued) Dr. Katherine Gibson www.umbc.edu Last Class We Covered Review of inheritance Overriding (vs overloading) Understanding polymorphism Limitations of


slide-1
SLIDE 1

www.umbc.edu

CMSC202 Computer Science II for Majors

Lecture 15 –

Polymorphism (Continued)

  • Dr. Katherine Gibson
slide-2
SLIDE 2

www.umbc.edu

Last Class We Covered

  • Review of inheritance
  • Overriding (vs overloading)
  • Understanding polymorphism

–Limitations of inheritance –Virtual functions –Abstract classes & function types

2

slide-3
SLIDE 3

www.umbc.edu

Any Questions from Last Time?

slide-4
SLIDE 4

www.umbc.edu

Common Project 3 Error

  • Have you seen something like this?

g++ -Wall -ansi –g -c test1.cpp In file included from test1.cpp:4: TrainCar.h:49: error: expected constructor, destructor,

  • r type conversion before ‘&’ token
  • The error is not in TrainCar.h!

– Where is it?

  • The “error” is in test1.cpp
  • Do not change the TrainCar.h file!

4

slide-5
SLIDE 5

www.umbc.edu

Today’s Objectives

  • Review of polymorphism

–Limitations of inheritance –Virtual functions –Abstract classes & function types

  • Finishing polymorphism

–Virtual function Tables –Virtual destructors/constructors

  • Livecoding application

5

slide-6
SLIDE 6

www.umbc.edu

Review of Virtual and Polymorphism

slide-7
SLIDE 7

www.umbc.edu

Overview of Polymorphism

  • Assume we have Vehicle *vehiclePtr = &myCar;
  • And this method call: vehiclePtr->Drive();

7

prototype Vehicle class Car class

void Drive()

  • Can implement function
  • Can create Vehicle
  • Can implement function
  • Can create Car
  • Calls Vehicle::Drive

virtual void Drive()

  • Can implement function
  • Can create Vehicle
  • Can implement function
  • Can create Car
  • Calls Car::Drive

virtual void Drive() = 0

  • Cannot implement function
  • Cannot create Vehicle
  • Must implement function
  • Can create Car
  • Calls Car::Drive
slide-8
SLIDE 8

www.umbc.edu

Overview of Polymorphism

  • Assume we have Vehicle *vehiclePtr = &myCar;
  • And this method call: vehiclePtr->Drive();

8

prototype Vehicle class Car class

void Drive()

  • Can implement function
  • Can create Vehicle
  • Can implement function
  • Can create Car
  • Calls Vehicle::Drive

virtual void Drive()

  • Can implement function
  • Can create Vehicle
  • Can implement function
  • Can create Car
  • Calls Car::Drive

virtual void Drive() = 0

  • Cannot implement function
  • Cannot create Vehicle
  • Must implement function
  • Can create Car
  • Calls Car::Drive
slide-9
SLIDE 9

www.umbc.edu

Overview of Polymorphism

  • Assume we have Vehicle *vehiclePtr = &myCar;
  • And this method call: vehiclePtr->Drive();

9

prototype Vehicle class Car class

void Drive()

  • Can implement function
  • Can create Vehicle
  • Can implement function
  • Can create Car
  • Calls Vehicle::Drive

virtual void Drive()

  • Can implement function
  • Can create Vehicle
  • Can implement function
  • Can create Car
  • Calls Car::Drive

virtual void Drive() = 0

  • Cannot implement function
  • Cannot create Vehicle
  • Must implement function
  • Can create Car
  • Calls Car::Drive

If no Car::Drive implementation, calls Vehicle::Drive This is a pure virtual function, and Vehicle is now an abstract class

slide-10
SLIDE 10

www.umbc.edu

Virtual Function Tables

slide-11
SLIDE 11

www.umbc.edu

Behind the Scenes

  • If our Drive() function is virtual,

how does the compiler know which child class’s version of the function to call?

SUV SUV Jeep Van Jeep Sedan Sedan SUV

vector of Car* objects

11

slide-12
SLIDE 12

www.umbc.edu

Virtual Function Tables

  • The compiler uses virtual function tables

whenever we use polymorphism

  • Virtual function tables are created for:

– Classes with virtual functions – Child classes of those classes

12

slide-13
SLIDE 13

www.umbc.edu

Virtual Table Pointer

SUV SUV Jeep Van Jeep Sedan Sedan Van

13

slide-14
SLIDE 14

www.umbc.edu

Virtual Table Pointer

  • The compiler adds a hidden variable

SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr

14

slide-15
SLIDE 15

www.umbc.edu

Virtual Table Pointer

  • The compiler also adds a virtual table of

functions for each class

SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr SUV virtual table Jeep virtual table Van virtual table Sedan virtual table

15

slide-16
SLIDE 16

www.umbc.edu

Virtual Table Pointer

  • Each virtual table has pointers to each
  • f the virtual functions of that class

SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr SUV virtual table * to SUV::Drive(); Jeep virtual table * to Jeep::Drive(); Van virtual table * to Van::Drive(); Sedan virtual table * to Sedan::Drive();

16

slide-17
SLIDE 17

www.umbc.edu

Virtual Table Pointer

  • The hidden variable points to the

appropriate virtual table of functions

SUV SUV Jeep Van Jeep Sedan Sedan Van *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr *__vptr SUV virtual table * to SUV::Drive(); Jeep virtual table * to Jeep::Drive(); Van virtual table * to Van::Drive(); Sedan virtual table * to Sedan::Drive();

17

slide-18
SLIDE 18

www.umbc.edu

Virtual Destructors/Constructors

slide-19
SLIDE 19

www.umbc.edu

Virtual Destructors

Vehicle *vehicPtr = new Car; delete vehicPtr;

  • For any class with virtual functions, you

must declare a virtual destructor as well

  • Why?
  • Non-virtual destructors will only

invoke the base class’s destructor

19

slide-20
SLIDE 20

www.umbc.edu

Virtual Constructors

  • Not a thing... why?
  • We use polymorphism and virtual functions to

manipulate objects without knowing type or having complete information about the object

  • When we construct an object,

we have complete information

– There’s no reason to have a virtual constructor

20

slide-21
SLIDE 21

www.umbc.edu

Livecoding

  • Animals (Bird, Cat, and Dog)

– All Animals can: Eat(), Speak(), and Perform()

  • Vector of Animal pointers – what happens?

21

slide-22
SLIDE 22

www.umbc.edu

Announcements

  • Project 3 is due tonight!
  • Exam 2 is in 1 week

– Will focus heavily on:

  • Classes
  • Inheritance
  • Linked Lists
  • Dynamic Memory
  • Some Polymorphism

22