1 / 18

Base class, Pet

// Pet is a base class class Pet { public : Pet(string name); string I(); void goes(); private : string voice(); string my_name; };. Pet::Pet(string name) :my_name(name) {} string Pet::I() { return my_name; } string Pet::voice() { return "???"; }

trixie
Télécharger la présentation

Base class, Pet

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. // Pet is a base class class Pet { public: Pet(string name); string I(); void goes(); private: string voice(); string my_name; }; Pet::Pet(string name) :my_name(name) {} string Pet::I() { return my_name; } string Pet::voice() { return "???"; } void Pet::goes() { cout << I() << " goes " << voice() << "!\n"; } int main() { Pet t("Tommy"); t.goes(); ...... } Base class, Pet Tommy goes ???!

  2. // Pet, base class class Pet { public: Pet(string name); string I(); void goes(); private: string voice(); string my_name; }; // Dog, derived class class Dog: public Pet { public: Dog(string name); private: string voice(); }; // Cat, derived class class Cat: public Pet { public: Cat(string name); private: string voice(); }; Derived classes, Cat & Dog string Dog::voice() { return "Woof!! Woof!"; } string Cat::voice() { return "Meow!! Meow!"; } redefinevoice() inheritgoes() call this goes() int main() { Dog d("Lucky"); Cat c("Mimi"); d.goes(); c.goes(); ........ } Lucky goes ???! Mimi goes ???!

  3. // Pet, base class class Pet { public: Pet(string name); string I(); void goes(); private: string voice(); string my_name; }; // Dog, derived class class Dog: public Pet { public: Dog(string name); void goes(); private: string voice(); }; // Cat, derived class class Cat: public Pet { public: Cat(string name); void goes(); private: string voice(); }; One solution: Redefine goes() & voice() call this voice() call this voice() string Dog::voice() { return "Woof!! Woof!"; } string Cat::voice() { return "Meow!! Meow!"; } int main() { Dog d("Lucky"); Cat c("Mimi"); d.goes(); c.goes(); ........ } Lucky goes Woof!! Woof!! Mimi goes Meow!! Meow!!

  4. Polymorphism: A better solution: Polymorphism allows a function to carry more than one definitions, and which one should be used is not determined during the compiling time. Namely, a proper definition is bonded to the function during the run time -- a.k.a. Dynamic Binding, Late Binding. In C++, we call such functions -- virtual functions, they don't have to be actually defined during the compiling time.

  5. when goes() calls voice(), the definition of voice() will be inherited backwards from the calling class. How this works: virtual function Pet:: I(); goes(); voice(); Dog:: I(); goes();voice(); Cat:: I();goes();voice(); Wolf:: I(); goes();voice(); BigCat:: I();goes();voice();

  6. // Cat, derived class class Cat: public Pet { public: Cat(string name); private: string voice(); }; // Dog, derived class class Dog: public Pet { public: Dog(string name); private: string voice(); }; // Pet, base class class Pet { public: Pet(string name); string I(); void goes(); private: virtual string voice(); string my_name; }; Virtual functions string Dog::voice() { return "Woof!! Woof!"; } string Cat::voice() { return "Meow!! Meow!"; } redefinevoice() inheritgoes() int main() { Dog d("Lucky"); Cat c("Mimi"); d.goes(); c.goes(); ........ } Lucky goes Woof!! Woof!! Mimi goes Meow!! Meow!!

  7. Virtual function, overriding Function, defining need to redefine, otherwise likes() defined in the base class will be inherited and it will call the food() defined in the bases class, which is not virtual. Inheritance call Change non-virtual to virtual call Overloading Mammal:: likes();food(); goes();voice(); likes();food(); goes();voice(); likes();food(); goes();voice(); Cat:: Dog:: call likes();food(); goes();voice(); likes();food(); goes();voice(); Wolf:: Lion::

  8. Virtual function, overriding Function, defining Inheritance • The function f() is • too complicate, but • identical, • for derived class designers to program. Also, • f() needs to call function g(), • and g() needs to be redefined in different derived classes. • In this case, g() should be a virtual function so the function f() called from a derived class can call the appropriate g() for the derived class. Why virtual functions? Base class::f(); g(); Derived 1::f();g(); Derived 2::f();g(); Derived 3::f();g(); Derived 4::f();g();

  9. A class contains pure virtual functions. An Abstract class: // Pet, abstract class class Pet { public: ...... private: virtual string voice() = 0; ..... }; There is no need to actually define a pure virtual function. • An abstract class can't be instantiated, but only be used as a base class. • A class remains abstract if it inherits an undefined pure virtual function and the pure function remains undefined in the class.

  10. Sample Program: #include <iostream> #include <string> using namespace std; // Base class class Pet { public: Pet(string name); // constructor void goes(); // NOT a virtual function void likes(); // NOT a virtual function string I(); // NOT a virtual function private: virtual string voice()=0; // a pure virtual fun, polymorphic string food(); // NOT a virtual function string my_name; };

  11. Pet::Pet(string name) :my_name(name) {} void Pet::goes() { cout << I() << " goes " << voice() << "!\n"; } void Pet::likes() { cout << I() << " likes " << food() << "!\n"; } string Pet::I() { return my_name; } string Pet::food() { return "????"; } Pet::

  12. // Derived Class class Dog: public Pet { public: Dog(string name); // constructor private: string voice(); // overriding string food(); // redefining }; Dog::Dog(string name) :Pet(name) {} string Dog::voice() { return "Woof!! Woof!"; } string Dog::food() { return "bone"; } Dog::

  13. // Derived Class class Cat: public Pet { public: Cat(string name); // constructor void likes(); // redefining private: string voice(); // overriding virtual string food(); // changing to virtual and redefining it }; Cat::Cat(string name) :Pet(name) {} void Cat::likes() { cout << I() << " likes " << food() << "!\n"; } string Cat::voice() { return "Meow!! Meow!";} string Cat::food() { return "fish"; } Cat::

  14. // Derived Class class BigCat: public Cat { public: BigCat(string name); // constructor private: string voice(); // overriding string food(); // overriding }; BigCat::BigCat(string name) :Cat(name) {} string BigCat::voice() { return "Grrr!! Grrr!"; } string BigCat::food() { return "zebras"; } BigCat::

  15. int main() { // Pet tommy("Tommy"); // Abstract class, can't be instantiated Dog lucky("Lucky"); cout << "\n" << lucky.I() << " is a Dog.\n"; lucky.goes(); lucky.likes(); Cat mimi("Mimi"); cout << "\n" << mimi.I() << " is a Cat.\n"; mimi.goes(); mimi.likes(); BigCat simba("Simba"); cout << "\n" << simba.I() << " is a BigCat.\n"; simba.goes(); simba.likes(); cout << endl; return 0; } Main Lucky is a Dog. Lucky goes Woof!! Woof!! Lucky likes ????! Mimi is a Cat. Mimi goes Meow!! Meow!! Mimi likes fish! Simba is a BigCat. Simba goes Grrr!! Grrr!! Simba likes zebras!

  16. Base class -- Cat:: fish Meow likes();food();goes();voice(); int main() { Cat mimi("Mimi"), BigCat simba("Simba"); Cat t("temp"); t = mimi; t.goes(); t.likes(); cout << endl; simba.goes(); simba.likes(); cout << endl; t = simba; t.goes(); t.likes(); .... } Assignment and Virtual functions virtual virtual likes();food(); goes();voice(); zebras Grrrr Derived class -- BigCat:: Mimi goes Meow!! Meow!! Mimi likes fish! Simba goes Grrr!! Grrr!! Simba likes zebras! Simba goes Meow!! Meow!! Simba likes fish!

  17. Base class -- Cat:: fish Meow likes();food();goes();voice(); int main() { ..... Cat mimi("Mimi"), BigCat simba("Simba"); Cat *p; p = &mimi; p->goes(); p->likes(); cout << endl; p = &simba; p->goes(); p->likes(); .... } Pointers and Virtual functions virtual virtual likes();food(); goes();voice(); zebras Grrrr Derived class -- BigCat:: Mimi goes Meow!! Meow!! Mimi likes fish! Simba goes Grrr!! Grrr!! Simba likes zebras!

  18. Trade-off: Efficiency A virtual function uses dynamic binding (in other words, late binding). This requires more storage and makes the program runs slower. • In C++, by default, a member function is not virtual functions unless specified by the modifier, virtual. • in Java, all member functions are virtual.

More Related