CS 162 Intro to Programming II Polymorphism Ia 1 - - PowerPoint PPT Presentation

cs 162 intro to programming ii
SMART_READER_LITE
LIVE PREVIEW

CS 162 Intro to Programming II Polymorphism Ia 1 - - PowerPoint PPT Presentation

CS 162 Intro to Programming II Polymorphism Ia 1 Virtual Func:ons A pointer to a derived (child) class is type- compa:ble with a pointer to


slide-1
SLIDE 1

CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡

Polymorphism ¡Ia ¡

1 ¡

slide-2
SLIDE 2

Virtual ¡Func:ons ¡

A ¡pointer ¡to ¡a ¡derived ¡(child) ¡class ¡is ¡type-­‑ compa:ble ¡with ¡a ¡pointer ¡to ¡its ¡base ¡(parent) ¡class ¡

  • eg. ¡

¡

Wizard *w1 = new Wizard(“Larry",100, 20,10,100); Character *c1 = w1;

2 ¡

slide-3
SLIDE 3

Virtual ¡Func:ons ¡

You ¡can ¡call ¡any ¡member ¡func:on ¡of ¡the ¡Character ¡ class ¡using ¡the ¡c1 ¡object ¡eg. ¡

Wizard *w1 = new Wizard(“Larry",100, 20,10,100); Character *c1 = w1; std::cout << c1->getName(); std::cout << c1->getStrength(); std::cout << c1->getIntelligence(); std::cout << c1->getHitPoints();

¡

3 ¡

slide-4
SLIDE 4

Virtual ¡Func:ons ¡

4 ¡

You ¡can ¡call ¡any ¡member ¡func:on ¡of ¡the ¡Character ¡ class ¡using ¡the ¡c1 ¡object ¡eg. ¡

Wizard *w1 = new Wizard(“Larry",100, 20,10,100); Character *c1 = w1; c1->heal(Bob,100); <<< Will not compile. Heal is part of Wizard class w1->heal(Bob,100); <<< This will work

¡

slide-5
SLIDE 5

Virtual ¡Func:ons ¡

  • What ¡if ¡you ¡call ¡the ¡aKack ¡func:on? ¡
  • Which ¡gets ¡called, ¡the ¡one ¡in ¡Wizard ¡or ¡the ¡one ¡in ¡

Character? ¡ ¡ ¡ ¡

Wizard *w1 = new Wizard(“Larry",100, 20,10,100); Character *c1 = w1; c1->attack(Bob);

The ¡aKack ¡func:on ¡in ¡Character ¡is ¡called. ¡ ¡ ¡

¡

5 ¡

slide-6
SLIDE 6

Virtual ¡Func:ons ¡

Wizard *w1 = new Wizard(“Larry",100, 20,10,100); Character *c1 = w1; c1->attack(Bob);

  • This ¡may ¡not ¡be ¡what ¡you ¡intend. ¡ ¡ ¡
  • c1 ¡points ¡to ¡a ¡Wizard ¡object ¡and ¡you ¡want ¡

that ¡aKack ¡func:on ¡called. ¡ ¡ ¡ ¡

6 ¡

slide-7
SLIDE 7

Virtual ¡Func:ons ¡

You ¡can ¡change ¡this ¡be ¡making ¡the ¡aKack ¡ func:on ¡declara:on ¡virtual ¡in ¡Character ¡by ¡ making ¡this ¡change ¡to ¡the ¡.hpp ¡file: ¡ ¡ ¡

¡

/* Character.hpp */ class Character { Public: Virtual void attack(Character& c); /* … */ }

You ¡do ¡not ¡need ¡the ¡virtual ¡keyword ¡in ¡the ¡.cpp ¡file ¡ ¡

7 ¡

slide-8
SLIDE 8

Virtual ¡Func:ons ¡

By ¡making ¡this ¡change ¡the ¡following ¡code ¡ now ¡calls ¡the ¡aKack ¡func:on ¡from ¡the ¡ Wizard ¡class: ¡ ¡ ¡

Wizard *w1 = new Wizard(“Larry",100, 20,10,100); Character *c1 = w1; c1->attack(Bob);

8 ¡

slide-9
SLIDE 9

Virtual ¡Func:ons ¡

  • The ¡previous ¡code ¡is ¡an ¡example ¡of ¡

polymorphism ¡ ¡

  • Polymorphism ¡allows ¡the ¡aKack() ¡func:on ¡to ¡

take ¡different ¡forms ¡depending ¡on ¡the ¡actual ¡ type ¡of ¡the ¡derived ¡class ¡

  • More ¡generally, ¡polymorphism ¡allows ¡you ¡to ¡

write ¡a ¡generic ¡piece ¡of ¡code ¡that ¡can ¡be ¡ applied ¡to ¡objects ¡of ¡different ¡types ¡

9 ¡

slide-10
SLIDE 10

Virtual ¡Func:ons ¡

  • Virtual ¡func:ons ¡allow ¡late ¡binding ¡(also ¡called ¡

dynamic ¡binding) ¡which ¡is ¡a ¡key ¡part ¡of ¡ polymorphism ¡

  • Late ¡binding ¡is ¡the ¡technique ¡of ¡wai:ng ¡un:l ¡

run ¡:me ¡(not ¡compile ¡:me) ¡to ¡determine ¡ which ¡implementa:on ¡of ¡a ¡func:on ¡to ¡run ¡

  • As ¡opposed ¡to ¡sta:c ¡binding ¡ ¡ ¡
  • See ¡page ¡943 ¡

10 ¡

slide-11
SLIDE 11

Virtual ¡Func:ons ¡

¡

/* Character.hpp */ class Character { Public: Virtual void attack(Character& c); /* … */ }

attack(Character& c) was ¡declared ¡virtual ¡in ¡the ¡

parent ¡class ¡it ¡will ¡automa:cally ¡be ¡declared ¡virtual ¡in ¡ all ¡derived ¡classes. ¡ ¡You ¡do ¡not ¡need ¡to ¡declare ¡it ¡virtual ¡

  • yourself. ¡ ¡ ¡

Note: ¡The ¡text ¡recommends ¡doing ¡it ¡anyway. ¡ ¡ ¡

11 ¡

slide-12
SLIDE 12

Virtual ¡Func:ons ¡

  • Which ¡is ¡which, ¡for ¡changing ¡the ¡behavior ¡of ¡

inherited ¡func:ons? ¡ ¡ ¡

– Overriding ¡refers ¡to ¡doing ¡this ¡change ¡to ¡a ¡virtual ¡ func:on ¡ – Redefining ¡refers ¡to ¡doing ¡this ¡change ¡to ¡a ¡non-­‑ virtual ¡func:on ¡ – Overloading ¡refers ¡to ¡the ¡defini:on ¡of ¡different ¡ func:ons ¡within ¡the ¡same ¡class ¡with ¡the ¡same ¡ name ¡and ¡different ¡ ¡parameter ¡lists. ¡

12 ¡

slide-13
SLIDE 13

Virtual ¡Func:ons ¡

  • Why ¡not ¡make ¡ALL ¡func:ons ¡virtual? ¡ ¡ ¡

– There ¡is ¡some ¡overhead ¡involved ¡when ¡you ¡ declare ¡a ¡func:on ¡to ¡be ¡virtual ¡ – Virtual ¡func:ons ¡are ¡slower ¡and ¡use ¡up ¡slightly ¡ more ¡memory ¡than ¡non-­‑virtual ¡ones ¡ – Only ¡declare ¡func:ons ¡to ¡be ¡virtual ¡if ¡required ¡

13 ¡