CS 162 Intro to Programming II Inheritance I 1 Basic - - PowerPoint PPT Presentation

cs 162 intro to programming ii
SMART_READER_LITE
LIVE PREVIEW

CS 162 Intro to Programming II Inheritance I 1 Basic - - PowerPoint PPT Presentation

CS 162 Intro to Programming II Inheritance I 1 Basic Idea Parent/base/super class Character More general Child/derived/sub class


slide-1
SLIDE 1

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

Inheritance ¡I ¡

1 ¡

slide-2
SLIDE 2

Basic ¡Idea ¡

¡ Parent/base/super ¡class ¡ ¡ ¡ ¡ ¡ ¡More ¡general ¡ ¡ ¡ Child/derived/sub ¡class ¡ ¡ ¡ ¡ ¡More ¡specific ¡

2 ¡

Character ¡ Wizard ¡

slide-3
SLIDE 3

Inheritance ¡

The ¡derived ¡class ¡(child ¡class) ¡inherits: ¡

  • ¡All ¡the ¡member ¡variables ¡of ¡the ¡base ¡class ¡

(or ¡parent ¡class) ¡

  • ¡All ¡the ¡member ¡funcHons ¡of ¡the ¡base ¡class ¡

(or ¡parent ¡class) ¡ Why ¡is ¡inheritance ¡useful? ¡

  • ¡Code ¡reuse ¡(character ¡has ¡code ¡that ¡is ¡in ¡

common ¡with ¡derived ¡classes) ¡

3 ¡

slide-4
SLIDE 4

Interface ¡

/* ¡Character.hpp ¡*/ ¡ class ¡Character ¡{ ¡ public: ¡ Character(std::string ¡nameValue, ¡int ¡hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue); ¡ std::string ¡getName() ¡{ ¡return ¡name; ¡} ¡ int ¡getHitPoints() ¡{ ¡return ¡hitPoints; ¡} ¡ int ¡getStrength() ¡{ ¡return ¡strength; ¡} ¡ int ¡getIntelligence() ¡{ ¡return ¡intelligence; ¡} ¡ void ¡setHitPoints(int ¡hitPointsValue) ¡{ ¡hitPoints ¡= ¡hitPointsValue; ¡} ¡ void ¡aWack(Character& ¡c); ¡ private: ¡ std::string ¡name; ¡ int ¡hitPoints; ¡ int ¡strength; ¡ int ¡intelligence; ¡ } ¡

4 ¡

slide-5
SLIDE 5

ImplementaHon ¡

/* ¡Character.cpp ¡*/ ¡ Character::Character(std::string ¡nameValue, ¡int ¡ hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue) ¡: ¡ name(nameValue), ¡hitPoints(hitPointsValue), ¡ strength(strengthValue), ¡intelligence(intelligenceValue) ¡{ ¡ } ¡ void ¡Character::aWack(Character& ¡c) ¡{ ¡ int ¡damage ¡= ¡rand() ¡% ¡strength; ¡ int ¡newHitPoints ¡= ¡c.getHitPoints() ¡-­‑ ¡damage; ¡ if( ¡newHitPoints ¡< ¡0 ¡) ¡ newHitPoints ¡= ¡0; ¡ c.setHitPoints(newHitPoints); ¡ } ¡

5 ¡

slide-6
SLIDE 6

Inheritance ¡

¡/* ¡Wizard.hpp ¡*/ ¡

class ¡Wizard ¡: ¡public ¡Character ¡ ¡{ ¡ public: ¡ Wizard(std::string ¡nameValue, ¡int ¡hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue, ¡ int ¡magicPointsValue); ¡ void ¡heal(Character& ¡c, ¡int ¡numHitPoints); ¡ private: ¡ int ¡magicPoints; ¡ }; ¡

6 ¡

It ¡is ¡derived ¡(inherits) ¡ ¡ from ¡Character ¡

slide-7
SLIDE 7

Inheritance ¡

¡/* ¡Wizard.hpp ¡*/ ¡

class ¡Wizard ¡: ¡public ¡Character ¡ ¡{ ¡ public: ¡ Wizard(std::string ¡nameValue, ¡int ¡hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue, ¡ int ¡magicPointsValue); ¡ void ¡heal(Character& ¡c, ¡int ¡numHitPoints); ¡ private: ¡ int ¡magicPoints; ¡ }; ¡

7 ¡

It ¡inherits ¡from ¡Character ¡ Variables-­‑ ¡name, ¡hitPoints, ¡strength, ¡intelligence ¡ FuncHons-­‑ ¡getName(), ¡getHitPoints(), ¡getStrength(), ¡ ¡ ¡ ¡ ¡ ¡ ¡getIntelligence(), ¡setHitPOints ¡

slide-8
SLIDE 8

Inheritance ¡

¡/* ¡Wizard.hpp ¡*/ ¡

class ¡Wizard ¡: ¡public ¡Character ¡ ¡{ ¡ public: ¡ Wizard(std::string ¡nameValue, ¡int ¡hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue, ¡ int ¡magicPointsValue); ¡ void ¡heal(Character& ¡c, ¡int ¡numHitPoints); ¡ private: ¡ int ¡magicPoints; ¡ }; ¡

8 ¡

It ¡has ¡its ¡own ¡ ¡ constructor ¡

slide-9
SLIDE 9

Inheritance ¡

¡/* ¡Wizard.hpp ¡*/ ¡

class ¡Wizard ¡: ¡public ¡Character ¡ ¡{ ¡ public: ¡ Wizard(std::string ¡nameValue, ¡int ¡hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue, ¡ int ¡magicPointsValue); ¡ void ¡heal(Character& ¡c, ¡int ¡numHitPoints); ¡ private: ¡ int ¡magicPoints; ¡ }; ¡

9 ¡

It ¡adds ¡funcHon ¡heal() ¡ And ¡variable ¡magicPoints ¡

slide-10
SLIDE 10

Inheritance ¡

/* ¡Wizard.cpp ¡*/ ¡ Wizard::Wizard(std::string ¡nameValue, ¡int ¡hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue, ¡ int ¡magicPointsValue) ¡: ¡ Character(nameValue, ¡hitPointsValue, ¡strengthValue, ¡intelligenceValue), ¡ magicPoints(magicPointsValue) ¡{ ¡ } ¡ void ¡Wizard::heal(Character& ¡c, ¡int ¡numHitPoints) ¡{ ¡ int ¡newHitPoints ¡= ¡c.getHitPoints() ¡+ ¡numHitPoints; ¡ c.setHitPoints(newHitPoints); ¡ } ¡

10 ¡

Calls ¡the ¡Character ¡ ¡ constructor ¡

slide-11
SLIDE 11

Constructor ¡

Suppose ¡you ¡wrote ¡the ¡Wizard ¡constructor ¡ without ¡the ¡call ¡to ¡the ¡Character ¡constructor: ¡

Wizard::Wizard(std::string ¡nameValue, ¡int ¡ hitPointsValue, ¡int ¡strengthValue, ¡ ¡ int ¡intelligenceValue, ¡ int ¡magicPointsValue) ¡: ¡ magicPoints(magicPointsValue) ¡{ ¡ } ¡

What ¡happens? ¡ ¡ ¡

11 ¡

slide-12
SLIDE 12

Constructor ¡

  • It ¡calls ¡the ¡default ¡constructor ¡for ¡Character ¡ie. ¡

Character() ¡

  • If ¡the ¡default ¡constructor ¡for ¡Character ¡doesn’t ¡

exist, ¡it ¡won’t ¡compile ¡

  • RecommendaHon: ¡Always ¡explicitly ¡invoke ¡the ¡

base ¡class ¡constructor ¡in ¡the ¡iniHalizaHon ¡secHon ¡

  • f ¡the ¡derived ¡class ¡constructor ¡
  • The ¡call ¡to ¡the ¡base ¡class ¡constructor ¡is ¡the ¡first ¡

thing ¡done ¡in ¡the ¡derived ¡class ¡constructor ¡

12 ¡

slide-13
SLIDE 13

Inheritance ¡ ¡

  • The ¡red ¡part ¡of ¡the ¡code ¡below ¡from ¡

Wizard.cpp ¡seems ¡strange. ¡

void ¡Wizard::heal(Character& ¡c, ¡int ¡numHitPoints) ¡{ ¡ int ¡newHitPoints ¡= ¡c.getHitPoints() ¡+ ¡numHitPoints; ¡ c.setHitPoints(newHitPoints); ¡ } ¡

  • Why ¡not ¡just ¡call ¡c.hitPoints? ¡

void ¡Wizard::heal(Character& ¡c, ¡int ¡numHitPoints) ¡{ ¡ int ¡newHitPoints ¡= ¡c.HitPoints ¡+ ¡numHitPoints; ¡ c.setHitPoints(newHitPoints); ¡ } ¡

13 ¡

slide-14
SLIDE 14

Inheritance ¡ ¡

  • The ¡red ¡part ¡of ¡the ¡code ¡below ¡from ¡

Wizard.cpp ¡seems ¡strange. ¡

void ¡Wizard::heal(Character& ¡c, ¡int ¡numHitPoints) ¡{ ¡ int ¡newHitPoints ¡= ¡c.getHitPoints() ¡+ ¡numHitPoints; ¡ c.setHitPoints(newHitPoints); ¡ } ¡

  • Why ¡not ¡just ¡call ¡c.hitPoints? ¡

void ¡Wizard::heal(Character& ¡c, ¡int ¡numHitPoints) ¡{ ¡ int ¡newHitPoints ¡= ¡c.HitPoints ¡+ ¡numHitPoints; ¡ c.setHitPoints(newHitPoints); ¡ } ¡

14 ¡

Not ¡allowed ¡as ¡hitPoints ¡ Is ¡private ¡

slide-15
SLIDE 15

Access ¡

  • Private ¡member ¡variables ¡and ¡member ¡

funcHons ¡are ¡inherited ¡but ¡not ¡accessible ¡to ¡ the ¡derived ¡class ¡

– The ¡derived ¡class ¡needs ¡to ¡call ¡the ¡public ¡ accessors ¡and ¡mutators ¡of ¡the ¡base ¡class ¡to ¡ change ¡private ¡member ¡variables ¡

  • Protected ¡member ¡variables ¡and ¡member ¡

funcHons ¡are ¡accessible ¡by: ¡

– The ¡class ¡itself ¡ – And ¡any ¡derived ¡classes ¡

15 ¡

slide-16
SLIDE 16

Interface ¡

/* ¡Character.hpp ¡*/ ¡ class ¡Character ¡{ ¡ public: ¡ Character(std::string ¡nameValue, ¡int ¡hitPointsValue, ¡ int ¡strengthValue, ¡int ¡intelligenceValue); ¡ std::string ¡getName() ¡{ ¡return ¡name; ¡} ¡ int ¡getHitPoints() ¡{ ¡return ¡hitPoints; ¡} ¡ int ¡getStrength() ¡{ ¡return ¡strength; ¡} ¡ int ¡getIntelligence() ¡{ ¡return ¡intelligence; ¡} ¡ void ¡setHitPoints(int ¡hitPointsValue) ¡{ ¡hitPoints ¡= ¡hitPointsValue; ¡} ¡ void ¡aWack(Character& ¡c); ¡ protected: ¡ std::string ¡name; ¡ int ¡hitPoints; ¡ int ¡strength; ¡ int ¡intelligence; ¡ } ¡

16 ¡

Now ¡accessible ¡to ¡drevied ¡classes ¡

slide-17
SLIDE 17

Inheritance ¡

  • The ¡Wizard ¡class ¡inherits ¡the ¡following ¡

funcHons ¡from ¡the ¡Character ¡class ¡

  • int ¡getName() ¡
  • int ¡getHitPoints() ¡
  • int ¡getStrength() ¡
  • int ¡getIntelligence() ¡
  • void ¡setHitPoints(int ¡hitPointsValue) ¡
  • void ¡a:ack(Character& ¡c); ¡
  • Any ¡of ¡the ¡above ¡can ¡be ¡called ¡by ¡a ¡Wizard ¡
  • bject ¡

17 ¡

slide-18
SLIDE 18

Inheritance ¡

  • This ¡code ¡will ¡call ¡the ¡aWack() ¡funcHon ¡from ¡the ¡

Character ¡class ¡

  • You ¡can ¡redefine ¡the ¡aWack() ¡funcHon ¡in ¡the ¡Wizard ¡

class ¡

  • In ¡fact, ¡you ¡can ¡redefine ¡any ¡inherited ¡funcHon ¡from ¡

Character ¡

¡

Wizard ¡w(“Gandalf”,100,10,10,10); ¡ Character ¡c(“Bob”,100,10,10); ¡ w.a:ack(c); ¡

18 ¡

slide-19
SLIDE 19

What ¡

/* ¡Wizard.hpp ¡*/ ¡ class ¡Wizard ¡: ¡public ¡Character ¡{ ¡ public: ¡ /* ¡code ¡in ¡here ¡*/ ¡ void ¡aWack(Character& ¡c); ¡ private: ¡ /* ¡etc. ¡*/ ¡ }; ¡ /* ¡Wizard.cpp ¡*/ ¡ void ¡Wizard::aWack(Character& ¡c) ¡{ ¡ int ¡damage ¡= ¡1; ¡ int ¡newHitPoints ¡= ¡c.getHitPoints() ¡-­‑ ¡damage; ¡ if( ¡newHitPoints ¡< ¡0 ¡) ¡ newHitPoints ¡= ¡0; ¡ c.setHitPoints(newHitPoints); ¡ } ¡

19 ¡

slide-20
SLIDE 20

Overload ¡vs ¡Redefining ¡

  • Important ¡disHncHon-­‑ ¡ ¡

Redefining ¡a ¡funcHon ¡definiHon: ¡

  • the ¡new ¡funcHon ¡definiHon ¡in ¡the ¡derived ¡class ¡

has ¡the ¡same ¡number ¡and ¡types ¡of ¡parameters ¡ Overloading ¡a ¡funcHon: ¡

  • The ¡funcHon ¡in ¡the ¡derived ¡class ¡has ¡a ¡different ¡

number ¡of ¡parameters ¡or ¡different ¡types ¡of ¡ parameters ¡from ¡the ¡funcHon ¡in ¡the ¡base ¡class ¡

20 ¡

slide-21
SLIDE 21

Overloading ¡

  • You ¡can ¡overload ¡a ¡new ¡aWack() ¡funcHon ¡for ¡a ¡

Wizard ¡object ¡

  • Note ¡it ¡has ¡two ¡parameters ¡ ¡
  • Wizard ¡objects ¡now ¡have ¡2 ¡aWack() ¡funcHons ¡

class ¡Wizard ¡: ¡public ¡Character ¡{ ¡ public: ¡ void ¡a:ack(Character& ¡c); ¡ void ¡a:ack(Character& ¡c, ¡bool ¡useMagic); ¡

21 ¡

slide-22
SLIDE 22

Inheritance ¡

  • Suppose ¡the ¡Wizard ¡class ¡had ¡only ¡one ¡aWack ¡

funcHon ¡declared ¡in ¡Wizard.hpp: ¡ void ¡a:ack(Character& ¡c, ¡bool ¡useMagic); ¡

  • Then ¡the ¡Wizard ¡class ¡would ¡sHll ¡have ¡two ¡

aWack ¡funcHons ¡

  • 1. One ¡aWack ¡funcHon ¡(with ¡two ¡parameters) ¡from ¡

the ¡Wizard ¡class ¡

  • 2. Another ¡aWack ¡funcHon ¡(with ¡one ¡parameter) ¡

from ¡the ¡Character ¡class ¡

22 ¡