{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

27+-+Final+Exam+Review - Outline Subtypes and Subclasses...

Info icon This 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
Image of page 1

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

View Full Document Right 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 r 's apparent type is "reference-to-IntSet", this code calls IntSet::insert() , which happily inserts -1 . This breaks the representational invariant of the set s , which is Very Bad . Subclasses Leaving behind subtypes 9 There is a way to tell C++ to choose dynamically.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern