27 - Final Exam Review

# 27 - Final Exam Review - 9/1/10 1 Final Exam Review EECS...

This preview shows pages 1–3. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9/1/10 1 Final Exam Review EECS 280 Programming and Introductory Data Structures 1 Outline 2 Subtypes and Subclasses Interfaces and Invariants Memory Models Copying Arrays Deep Copies and Resizing Linked Lists Templates Polymorphism and Bugs Iterators Functors Subclasses Creating Subclasses using inheritance 3 class MaxIntSet : public IntSet { // OVERVIEW: a set of non-negative integers, // where |set| &lt;= 100 public: int max(); // REQUIRES: set is non-empty // EFFECTS: returns largest element in set. }; This creates a new type that has all of the behavior of IntSet , plus one new operation. We say that MaxIntSet is a &quot;derived class&quot;, and it is &quot;derived from&quot; IntSet . It automatically inherits all of the IntSet methods and data elements. Subclasses 4 int MaxIntSet::max() { int i; for (i=INT_MAX; i&gt;=INT_MIN; i--) { if ( query(i) ) return i; } } This function is correct, but inefficient. We'll have to query 2 31 (i.e. of 2 32 ) numbers on average to find the maximum element in a randomly-constructed singleton set! Subclasses 5 int MaxIntSet::max() { int i; for (i=INT_MAX; i&gt;=INT_MIN; i--) { if ( query(i) ) return i; } } C++ has a mechanism that allows us to get around this problem The &quot;protected&quot; storage class. If a member is &quot;protected&quot;, it means &quot;can be seen by all members of this class and any derived classes&quot;. Subclasses 6 class IntSet { protected: // all of the data members plus indexOf public: // the public interface to the class. }; Since MaxIntSet is derived from IntSet , the protected members of IntSet are visible to MaxIntSet . Customers&quot; of the class still cannot see the members. With this new structure, we can write max much more efficiently. 9/1/10 2 Subclasses 7 int MaxIntSet::max() { int so_far = elts[0]; for (int i = 1; i&lt;numElts; i++) { if (elts[i] &gt; so_far) so_far = elts[i]; } return so_far; } int MaxIntSet::max() { return elts[numElts-1]; } Unsorted Array Sorted Array Subclasses 8 class SafeMaxIntSet : public MaxIntSet { // OVERVIEW: a mutable set of integers, // where |set| &lt;= 100 public: int max(); // EFFECTS: if set is non-empty, returns largest // element in set // otherwise, returns INT_MIN. }; This defines a new class that is exactly like a MaxIntSet , except that it replaces or &quot;overrides&quot; the method max . The compiler decides which max to call by the type of the object to which we are sending the max message. Subclasses 9 So, if we declared one set of each type, calling the max method would give us the &quot;right&quot; one: MaxIntSet ms; SafeMaxIntSet ss; ss.max(); // calls SafeMaxIntSet::max() // returns INT_MIN on empty set ms.max(); // calls MaxIntSet::max() // is undefined on empty set 10 The rules of C++ allow a subclass to be used wherever a superclass is expected....
View Full Document

## This note was uploaded on 12/14/2010 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

### Page1 / 18

27 - Final Exam Review - 9/1/10 1 Final Exam Review EECS...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online