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

cs 162 intro to programming ii
SMART_READER_LITE
LIVE PREVIEW

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

CS 162 Intro to Programming II Polymorphism II 1 Virtual Func:ons Which is which, for changing the behavior of inherited func:ons?


slide-1
SLIDE 1

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

Polymorphism ¡II ¡

1 ¡

slide-2
SLIDE 2

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. ¡

2 ¡

slide-3
SLIDE 3

Abstract ¡Classes ¡

/* Shape.hpp */ class Shape { public: Shape(); virtual void print() = 0; virtual double getArea() = 0; }; /* Shape.cpp */ Shape::Shape() { }

3 ¡

slide-4
SLIDE 4

Abstract ¡Classes ¡

/* Shape.hpp */ class Shape { public: Shape(); virtual void print() = 0; virtual double getArea() = 0; }; ¡

  • These ¡are ¡pure ¡virtual ¡func:ons ¡ ¡ ¡
  • Cannot ¡create ¡a ¡shape ¡object-­‑ ¡abstract ¡class ¡

4 ¡

slide-5
SLIDE 5

Abstract ¡Classes ¡

  • Why ¡abstract ¡classes? ¡ ¡ ¡

– It ¡has ¡the ¡common ¡characteris:cs ¡ ¡ – Details ¡are ¡leP ¡to ¡specific ¡subclasses ¡ ¡ – In ¡this ¡case ¡we ¡want ¡each ¡shape ¡to ¡draw ¡itself ¡and ¡ compute ¡its ¡area ¡ ¡ – Details ¡will ¡vary ¡for ¡each ¡shape ¡ ¡ – You ¡do ¡not ¡even ¡need ¡to ¡know ¡what ¡shapes ¡may ¡ be ¡used ¡later! ¡ ¡ ¡

5 ¡

slide-6
SLIDE 6

Abstract ¡Classes ¡

  • A ¡derived ¡class ¡will ¡also ¡be ¡abstract ¡unless ¡you: ¡ ¡
  • 1. You ¡provide ¡defini:ons ¡for ¡the ¡inherited ¡pure ¡virtual ¡

func:ons ¡

  • 2. You ¡do ¡not ¡add ¡any ¡pure ¡virtual ¡func:ons ¡
  • If ¡ ¡these ¡condi:ons ¡are ¡met ¡you ¡can ¡create ¡an ¡
  • bject ¡of ¡the ¡derived ¡class ¡ ¡

6 ¡

slide-7
SLIDE 7

Abstract ¡Classes ¡

  • How ¡does ¡this ¡work? ¡ ¡ ¡
  • We ¡will ¡create ¡a ¡derived ¡class ¡for ¡a ¡Rectangle ¡

and ¡a ¡Triangle ¡

  • Both ¡will ¡define ¡the ¡pure ¡virtural ¡func:ons ¡ ¡
  • Both ¡will ¡add ¡member ¡variables ¡and ¡func:ons ¡

(but ¡no ¡pure ¡virtual ¡func:ons) ¡ ¡ ¡

7 ¡

slide-8
SLIDE 8

Abstract ¡Classes ¡

/* Rectangle.hpp */ class Rectangle : public Shape { public: Rectangle(double height, double width); void print(); double getArea(); private: double height; double width; };

8 ¡

slide-9
SLIDE 9

Abstract ¡Classes ¡

/* Inside Rectangle.cpp. Constructor omitted due of lack of space */ void Rectangle::print() { for( int i = 0; i < height; i++ ) { for( int j = 0; j < width; j++ ) { std::cout << "*"; } std::cout << std::endl; } } double Rectangle::getArea() { return (height*width); }

9 ¡

slide-10
SLIDE 10

Abstract ¡Classes ¡

/* Triangle.hpp */ class Rectangle : public Shape { public: Triangle(double height, double width); void print(); double getArea(); private: double height; double width; };

10 ¡

slide-11
SLIDE 11

Abstract ¡Classes ¡

/* Inside Triangle.cpp. Constructor omitted due of lack of space */ void Triangle::print() { for( int i = 0; i < height; i++ ) { for( int j = 0; j <= i; j++ ) { std::cout << "*"; } std::cout << std::endl; } } double Rectangle::getArea() { return (0.5*height*width); }

11 ¡

slide-12
SLIDE 12

Slicing ¡Problem ¡

/* In Bunny.hpp */ class Bunny { public: Bunny(std::string); virtual ~Bunny(); virtual void print() const; private: std::string *name; };

12 ¡

slide-13
SLIDE 13

Slicing ¡Problem ¡

/* In MutantBunny.hpp */ class MutantBunny : public Bunny { public: MutantBunny(std::string name); ~MutantBunny(); void print() const; void addNameOfBunnyEaten(std::string name); private: std::vector<std::string> *namesOfBunniesEaten; };

13 ¡

slide-14
SLIDE 14

Slicing ¡Problem ¡

¡/* In Bunny.cpp */

void Bunny::print() const { std::cout << "Name: " << name << std::endl; } ¡/* In MutantBunny.cpp*/ void MutantBunny::print() const { Bunny::print(); std::cout << "Bunnies eaten: " << std::endl; for( int i = 0; i < namesOfBunniesEaten->size();i++ ) { std::cout << "\t" << (*namesOfBunniesEaten)[i] << std::endl; } }

14 ¡

slide-15
SLIDE 15

Slicing ¡Problem ¡

MutantBunny mb("Fluffy"); Bunny b = mb;

  • This ¡assignment ¡of ¡objects ¡works ¡ ¡
  • The ¡copy ¡does ¡not ¡include ¡any ¡variables ¡in ¡

MutantBunny ¡but ¡not ¡in ¡Bunny ¡ ¡

  • It ¡slices ¡off ¡those ¡data ¡members ¡ ¡
  • This ¡code ¡will ¡not ¡compile ¡as ¡b ¡is ¡a ¡Bunny: ¡ ¡ ¡

b.addNameOfBunnyEaten(“Bugs”);

15 ¡

slide-16
SLIDE 16

Slicing ¡Problem ¡

MutantBunny *mb = new MutantBunny("Fluffy"); mb->addNameOfBunnyEaten("Bugs"); Bunny *b = mb; b->print();

  • This ¡assignment ¡of ¡pointers ¡works ¡ ¡
  • print() calls the version in ¡

MutantBunny ¡but ¡not ¡in ¡Bunny ¡ ¡

  • The ¡output ¡is: ¡ ¡ ¡

Name: Fluffy Bunnies eaten: Bugs

16 ¡

slide-17
SLIDE 17

Virtual ¡Destructors ¡

  • In ¡Bunny.hpp ¡we ¡had: ¡ ¡

virtual ~Bunny();

  • The ¡defini:ons ¡look ¡like ¡this: ¡ ¡

/* In Bunny.cpp */ Bunny::~Bunny() { delete name; } /* In MutantBunny.cpp */ MutantBunny::~MutantBunny() { delete namesOfBunniesEaten; }

17 ¡

slide-18
SLIDE 18

Virtual ¡Destructors ¡

  • If ¡the ¡destructor ¡is ¡not ¡virtual ¡then ¡this ¡code: ¡ ¡

Bunny *b = new MutantBunny(“Bob”); delete b; ¡

  • Will ¡clean ¡up ¡the ¡name ¡member ¡in ¡Bunny ¡ ¡
  • But ¡not ¡the ¡namesOfBunniesEaten ¡in ¡

MutantBunny ¡

  • If ¡the ¡base ¡class ¡destructor ¡is ¡virtual ¡then ¡all ¡

derived ¡class ¡destructors ¡are ¡virtual ¡and ¡will ¡ be ¡called ¡

18 ¡