Inheritance Inheritance Many objects have a hierarchical relationship
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?
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
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 { … void print(); } class Student: public Person{ … void print(); }
Function Invocation • Person – print, get. Name • Student – print, change. Major Person p(…); Student s(…); s. get. Name(); p. print(); s. print(); p. change. Major(); s. change. Major();
Function Invocation • Person – print, get. Name • Student – print, change. Major Person p(…); Student s(…); s. get. Name(); //Person: : get. Name p. print(); //Person: : print s. print(); //Student: : print p. change. Major(); //ERROR!!!! s. change. Major(); //Student: : change. Major
More Syntax - Partial Overriding void Person: : print() {…} void Student: : print() { Person: : print(); //Superclass: : function(); … }
Protected • private members of parent not accessible to child class • protected members accessible only to derived classes • examples classname { private: protected: public: }
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; }
Static Binding Person* p = new Person(…); Student* s = new Student(…); p->print(); //calls Person: : print() p = s; //OKAY p->print(); //calls Person: : print() p->change. Major(); //ERROR • Function called depends on declared type
Example Magazine Book library Library. Item *library[10]; library[0] = new Magazine; library[1] = new Book; … library[9] = new Book; Book …
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(); }
Dyanmic Binding Person* p = new Person(…); Student* s = new Student(…); p->print(); //calls Person: : print() p = s; //OKAY p->print(); //calls Student: : print() p->change. Major(); //ERROR
Polymorphism • Many forms • A variable is polymorphic if it points to an object with 1 or more virtual functions
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
Abstract Classes • May want to defined base class which cannot be instantiated – Examples – bank account, car • Declare one or more functions as pure virtual void print() = 0; virtual void func_name(…) = 0;
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
Example Class 1 1. f 1 2. virtual f 2 Class 2: public Class 1 3. virtual f 2 4. f 3 Class 1 c 1; Class 2 c 2; c 1. f 1(); c 1. f 2(); c 2. f 3(); c 2. f 1(); c 1. f 3(); c 1 = c 2; c 1. f 3();
Example Class 1 1. f 1 2. virtual f 2 Class 2: public Class 1 3. virtual f 2 4. f 3 Class 1 c 1; Class 2 c 2; c 1. f 1(); //1 c 1. f 2(); //2 c 2. f 2(); //3 c 2. f 3(); //4 c 2. f 1(); //1 c 1. f 3(); //error c 1 = c 2; c 1. f 3(); //error
Example Class 1 1. f 1 2. virtual f 2 Class 2: public Class 1 3. virtual f 2 4. f 3 Class 1 *c 1_ptr = new Class 2(); Class 2 *c 2_ptr = new Class 2(); c 1_ptr->f 3(); c 1_ptr->f 2(); c 2_ptr = dynamic_cast<Class 2*>c 1_ptr; c 2_ptr->f 3();
Example Class 1 1. f 1 2. virtual f 2 Class 2: public Class 1 3. virtual f 2 4. f 3 Class 1 *c 1_ptr = new Class 2(); Class 2 *c 2_ptr = new Class 2(); c 1_ptr->f 3(); //error c 1_ptr->f 2(); //3 c 2_ptr = dynamic_cast<Class 2*>c 1_ptr; c 2_ptr->f 3(); //4
- Slides: 21