Inheritance: The Fundamental Functions

Similar documents
Inheritance: The Fundamental Functions

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Class Construct Part 2

The Class Construct Part 1

Recursive Linked Lists

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

Scope and Parameter Passing

Binary Tree Implementation

The Pairwise-Comparison Method

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Minimal Spanning Trees

Scheduling and Digraphs

Implementing Linked Lists

The Critical-Path Algorithm

Binary Tree Implementation

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Scope and Parameter Passing

Stacks and their Applications

Friends and Unary Operators

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Sampling Distribution Examples Sections 15.4, 15.5

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

List Iterator Implementation

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Solving Recursive Sequences by Iteration

Programming Languages

The Standard Template Library Classes

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The Plurality-with-Elimination Method

Magnification and Minification

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

The Decreasing-Time Algorithm

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

The Traveling Salesman Problem Brute Force Method

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Building the Abstract Syntax Trees

Binary Tree Applications

Density Curves Sections

LR Parsing - Conflicts

Rotations and Translations

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Programming Languages

Recognition of Tokens

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

Abstract Syntax Trees Synthetic and Inherited Attributes

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Recursive Descent Parsers

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Lecture 29 Section Wed, Apr 1, 2009

The x86 Instruction Set

PHP Queries and HTML Forms Lecture 23

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Fundamental Data Types

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

The Normal Distribution

Ambient and Diffuse Light

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

The Coefficient of Determination

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

Displaying Distributions - Quantitative Variables

AJAX. Lecture 26. Robb T. Koether. Fri, Mar 21, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) AJAX Fri, Mar 21, / 16

The Graphics Pipeline

Nondeterministic Programming in C++

The Graphics Pipeline

The x86 Architecture

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

MySQL Creating a Database Lecture 3

Dynamic Allocation of Memory

PHP Arrays. Lecture 18. Robb T. Koether. Hampden-Sydney College. Mon, Mar 4, 2013

DTDs and XML Attributes

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Street-Routing Problems

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Basic PHP. Lecture 19. Robb T. Koether. Hampden-Sydney College. Mon, Feb 26, 2108

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Introduction to Compiler Design

XQuery FLOWR Expressions Lecture 35

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

The Projection Matrix

Function Definition Syntax Tree

Javascript Lecture 23

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

LR Parsing - The Items

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

Functional Dependencies and Normal Forms

Transcription:

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