Inheritance: The Fundamental Functions Lecture 21 Sections 11.11-11.12 Robb T. Koether Hampden-Sydney College Mon, Mar 17, 2014 Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 1 / 26
1 Inheritance of Constructors 2 Inheritance of Destructors 3 Inheritance of the Assignment Operator 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 2 / 26
Outline 1 Inheritance of Constructors 2 Inheritance of Destructors 3 Inheritance of the Assignment Operator 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 3 / 26
Inheritance of Constructors Constructor Rules 1 A derived-class constructor will automatically invoke the base-class default constructor, unless instructed otherwise. 2 We may instruct the derived-class constructor to invoke a specific base-class constructor. 3 The base-class constructor is invoked before the derived-class constructor is executed. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 4 / 26
Invoking the Base Class Constructor Base Class Constructor Derived-class(parameters) : Base-class(parameters) { body of the Derived-class constructor } We may specify other constructors through an initializer. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 5 / 26
Inheritance of Constructors Example (A List Hierarchy) Suppose we create a List base class and then derive an ArrayList class and a LinkedList class from it. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 6 / 26
Inheritance of Constructors Example (A List Hierarchy) The ArrayList and LinkedList classes have msize in common. Therefore, we could put msize in the List base class. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 7 / 26
Inheritance of Constructors Example (A List Hierarchy) class List { public: List() {msize = 0;} List(int sz) {msize = sz;} int size() const {return msize;} bool isempty() const {return msize == 0;} protected: int msize; } Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 8 / 26
Inheritance of Constructors Example (A List Hierarchy) class ArrayList : public List { public: ArrayList() {} ArrayList(int sz, const T& value) : List(sz) {...} protected: T* element; } Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 9 / 26
Inheritance of Constructors Example (A List Hierarchy) class LinkedList : public List { public: LinkedList() {} LinkedList(int sz, const T& value) : List(sz) {...} protected: LinkedListNode* head; } Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 10 / 26
Inheritance of Constructors Example (A List Hierarchy) When we construct an ArrayList using ArrayList list(5, 123); what would happen? Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 11 / 26
Inheritance of Constructors Example (A List Hierarchy) When we construct an ArrayList using ArrayList list(5, 123); what would happen? The ArrayList constructor will first call a List constructor which will initialize msize. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 11 / 26
Inheritance of Constructors Example (A List Hierarchy) When we construct an ArrayList using ArrayList list(5, 123); what would happen? The ArrayList constructor will first call a List constructor which will initialize msize. Then it will initialize element. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 11 / 26
Outline 1 Inheritance of Constructors 2 Inheritance of Destructors 3 Inheritance of the Assignment Operator 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 12 / 26
Inheritance of Destructors Destructor Rules 1 The derived-class destructor automatically invokes the base-class destructor. 2 The base-class destructor is invoked after the derived-class destructor is executed. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 13 / 26
Outline 1 Inheritance of Constructors 2 Inheritance of Destructors 3 Inheritance of the Assignment Operator 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 14 / 26
Inheritance of the Assignment Operator Assignment Operator Rules 1 The automatic assignment operator invokes the assignment operator for the base class. 2 A programmer-defined assignment operator must copy the base-class members. This is a problem if the base-class members are private. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 15 / 26
Inheritance of the Assignment Operator Example (Inheritance of the Assignment Operator) class List { public: List& operator=(const List& lst); private: int msize; }; class ArrayList : public List { public: ArrayList& operator=(const ArrayList& lst); private: T* element; }; Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 16 / 26
Inheritance of the Assignment Operator Example (Inheritance of the Assignment Operator) ArrayList& operator=(const ArrayList& lst) { if (this!= &lst) { } msize = lst.msize for (int i = 0; i < msize; i++) element[i] = lst.element[i]; } return *this; // Wrong! Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 17 / 26
Inheritance of the Assignment Operator Example (Inheritance of the Assignment Operator) ArrayList& operator=(const ArrayList& lst) { if (this!= &lst) { } List::operator=(lst) for (int i = 0; i < size(); i++) element[i] = lst.element[i]; } return *this; // Right! The scope operator :: can be used to invoke the assignment operator of the base class. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 18 / 26
Inheritance of the Assignment Operator Example (Subclass of ArrayList) Suppose we create a class ArrayListwPos, which is an ArrayList that keeps track of a current position. ArrayListwPos has one additional data member: int currpos; Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 19 / 26
Inheritance of the Assignment Operator Example (Subclass of ArrayList) ArrayListwPos& operator=(const ArrayListwPos& list) { if (this!= &list) { ArrayList::operator=(list); currpos = list.currpos; } return *this; } Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 20 / 26
Outline 1 Inheritance of Constructors 2 Inheritance of Destructors 3 Inheritance of the Assignment Operator 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 21 / 26
Example Example (Inheritance) Create the following classes: Person Man Woman Father Mother MarriedMan MarriedWoman Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 22 / 26
Example Person Man Woman Married Man Father Mother Married Woman The classes Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 23 / 26
Example Person Man Woman Married Man Father Mother Married Woman The IS-A Relation Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 23 / 26
Example Person Man Woman Married Man Father Mother Married Woman The HAS-A Relation Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 23 / 26
Example Person Man Woman Married Man Father Mother Married Woman The whole shebang Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 23 / 26
Example Example (Inheritance) person.h man.h woman.h father.h mother.h marriedman.h marriedwoman.h Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 24 / 26
Outline 1 Inheritance of Constructors 2 Inheritance of Destructors 3 Inheritance of the Assignment Operator 4 Example 5 Assignment Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 25 / 26
Assignment Homework Read Section 11.11-11.12. Robb T. Koether (Hampden-Sydney College) Inheritance: The Fundamental Functions Mon, Mar 17, 2014 26 / 26