27+-+Final+Exam+Review

27 Final Exam Revi - Outline Subtypes and Subclasses EECS 280 Interfaces and Invariants Memory Models Programming and Introductory Data Structures

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

View Full Document Right Arrow Icon
4/17/11 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| <= 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 "derived class", and it is "derived from" IntSet . It automatically inherits all of the IntSet methods and data elements. Subclasses 4 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. int MaxIntSet::max() { int i; for (i=INT_MAX; i>=INT_MIN; i--) { if ( query(i) ) return i; } } Inefficient!! Subclasses 5 class SafeMaxIntSet : public MaxIntSet { // OVERVIEW: a mutable set of integers, // where |set| <= 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 "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 6 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
4/17/11 2 7 C++ allows a subclass to be used wherever a superclass is expected. This is perfectly legal: PosIntSet s; IntSet* p = &s; IntSet& r = s; However…what if you tried this: try { r.insert(-1); } catch (int i) { cout << "Error!\n”; } The type of r is declared to be “reference to an IntSet”, but it refers to a PosIntSet . Thus, the apparent type and the actual type differ. Apparent type: the declared type of the reference. Actual type: the real type of the referent. Subclasses 8 try { s.insert(-1); } catch (int i) { cout << "Exception thrown\n"; } try { r.insert(-1); } catch (int i) { cout << "Exception thrown\n"; } PosIntSet s; IntSet* p = &s; IntSet& r = s; This will do exactly what you expect: "Exception thrown\n". Because
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 16

27 Final Exam Revi - Outline Subtypes and Subclasses EECS 280 Interfaces and Invariants Memory Models Programming and Introductory Data Structures

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online