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
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 nonnegative integers, // where set <= 100 public: int max(); // REQUIRES: set is nonempty // 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 "derived class", and it is "derived from" IntSet . It automatically inherits all of the IntSet methods and data elements. Subclasses 4 int MaxIntSet::max() { int i; for (i=INT_MAX; i>=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 randomlyconstructed singleton set! Subclasses 5 int MaxIntSet::max() { int i; for (i=INT_MAX; i>=INT_MIN; i) { if ( query(i) ) return i; } } C++ has a mechanism that allows us to get around this problem The "protected" storage class. If a member is "protected", it means "can be seen by all members of this class and any derived classes". 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" 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<numElts; i++) { if (elts[i] > so_far) so_far = elts[i]; } return so_far; } int MaxIntSet::max() { return elts[numElts1]; } Unsorted Array Sorted Array Subclasses 8 class SafeMaxIntSet : public MaxIntSet { // OVERVIEW: a mutable set of integers, // where set <= 100 public: int max(); // EFFECTS: if set is nonempty, 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 "overrides" 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 "right" 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.
 Fall '08
 NOBLE
 Data Structures, Polymorphism

Click to edit the document details