cs 162 intro to programming ii
play

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?


  1. CS ¡162 ¡ Intro ¡to ¡Programming ¡II ¡ Polymorphism ¡II ¡ 1 ¡

  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 ¡

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

  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 ¡

  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 ¡

  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 ¡ object ¡of ¡the ¡derived ¡class ¡ ¡ 6 ¡

  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 ¡

  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 ¡

  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 ¡

  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 ¡

  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 ¡

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

  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 ¡

  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 ¡

  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 ¡

  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 ¡

  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 ¡

  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 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend