1 / 21

Inheritance

Inheritance. Inheritance. Many objects have a hierarchical relationship Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software design to take advantage of relationships, supporting reuse Supports the IS-A relationship what’s the HAS-A relationship?.

nguyet
Download Presentation

Inheritance

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

  2. Inheritance • Many objects have a hierarchical relationship • Examples: zoo, car/vehicle, card game, airline reservation system • Inheritance allows software design to take advantage of relationships, supporting reuse • Supports the IS-A relationship • what’s the HAS-A relationship?

  3. Terminology • Base class/Parent class/Superclass • defines generic functionality • Derived class/Child class/Subclass • extends or specializes base class • inherits members of parent • may implement new members • may override members of parent

  4. Syntax class Student : public Person {…} class Derived : public Base{…} • Derived class may override or reimplement a function implemented by base class • Public inheritance - public members remain public class Person { class Student:public Person{ … … void print(); void print(); } }

  5. Function Invocation • Person – print, getName • Student – print, changeMajor Person p(…); Student s(…); s.getName(); p.print(); s.print(); p.changeMajor(); s.changeMajor();

  6. Function Invocation • Person – print, getName • Student – print, changeMajor Person p(…); Student s(…); s.getName(); //Person::getName p.print(); //Person::print s.print(); //Student::print p.changeMajor(); //ERROR!!!!!!!! s.changeMajor(); //Student::changeMajor

  7. More Syntax - Partial Overriding void Person::print() {…} void Student::print() {Person::print(); //Superclass::function(); … }

  8. Protected • private members of parent not accessible to child class • protected members accessible only to derived classes • examples class classname {private: protected: public: }

  9. Child Class Constructors • Subclass must create superclass • invoke superclass constructor from subclass constructor • use initializer list Student::Student(string newname, string newmajor) :Person(newname), major(newmajor) {…} Student::Student(string newname, string newmajor) :Person(newname) {major = newmajor;}

  10. Static Binding Person* p = new Person(…); Student* s = new Student(…); p->print(); //calls Person::print() p = s; //OKAY p->print(); //calls Person::print() p->changeMajor(); //ERROR • Function called depends on declared type

  11. Example Magazine Book Book library … LibraryItem *library[10]; library[0] = new Magazine; library[1] = new Book; … library[9] = new Book;

  12. Dynamic Binding • May want to determine which function to call based on object contents • Use virtual functions class Person { virtual void print(); } class Student : public Person {virtual void print(); }

  13. Dyanmic Binding Person* p = new Person(…); Student* s = new Student(…); p->print(); //calls Person::print() p = s; //OKAY p->print(); //calls Student::print() p->changeMajor(); //ERROR

  14. Polymorphism • Many forms • A variable is polymorphic if it points to an object with 1 or more virtual functions

  15. Casting Person* p; p = new Student(…); Student* s = dynamic_cast<Student*>(p); • Create new pointer of subclass type to point to object • Pointer (s in this case) will be NULL if cast was NOT successful • Can also use C-style cast

  16. Abstract Classes • May want to defined base class which cannot be instantiated • Examples – bank account, car • Declare one or more functions as pure virtual virtual void print() = 0; virtual void func_name(…) = 0;

  17. Summary • Statically declared variables • Static binding always applies (virtual functions don’t matter) • Superclass variable can hold object of subclass type • Cannot call derived class functions if reference to object is of base class • Dynamically declared variables • Dynamic binding applies • virtual functions called will depend on object type • Superclass variable can hold object of subclass type • Cannot call non-virtual derived class functions on variable of type base class • After dynamic cast, can call subclass functions

  18. Example Class1 Class2:public Class1 1. f1 3. virtual f2 2. virtual f2 4. f3 Class1 c1; c2.f1(); Class2 c2; c1.f3(); c1.f1(); c1 = c2; c1.f2(); c1.f3(); c2.f2(); c2.f3();

  19. Example Class1 Class2:public Class1 1. f1 3. virtual f2 2. virtual f2 4. f3 Class1 c1; c2.f1(); //1 Class2 c2; c1.f3(); //error c1.f1(); //1 c1 = c2; c1.f2(); //2 c1.f3(); //error c2.f2(); //3 c2.f3(); //4

  20. Example Class1 Class2:public Class1 1. f1 3. virtual f2 2. virtual f2 4. f3 Class1 *c1_ptr = new Class2(); Class2 *c2_ptr = new Class2(); c1_ptr->f3(); c1_ptr->f2(); c2_ptr = dynamic_cast<Class2*>c1_ptr; c2_ptr->f3();

  21. Example Class1 Class2:public Class1 1. f1 3. virtual f2 2. virtual f2 4. f3 Class1 *c1_ptr = new Class2(); Class2 *c2_ptr = new Class2(); c1_ptr->f3(); //error c1_ptr->f2(); //3 c2_ptr = dynamic_cast<Class2*>c1_ptr; c2_ptr->f3(); //4

More Related