15 - Subtypes and Subclasses

15 - Subtypes and Subclasses - EECS 280 Programming and...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Click to edit Master subtitle style 12/15/10 Subtypes and Subclasses EECS 280 Programming and Introductory Data Structures 1 1
Image of page 1

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

View Full Document Right Arrow Icon
12/15/10 Subtypes Recall – Introduction Suppose we have two types, S and T. S is a subtype of T, written “S <: T”, if: For any instance where an object of type T is expected, an object of type S can be supplied without changing the correctness of the original computation. <or> Objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program (correctness). 2 2
Image of page 2
12/15/10 Subtypes Creating 3 3 In an Abstract Data Type, there are three ways to create a subtype from a supertype: 1. Weaken the precondition of one or more operations. 2. Strengthen the postcondition of one or more operations. 3. Add one or more operations.
Image of page 3

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

View Full Document Right Arrow Icon
12/15/10 Subtypes Creating 4 4 The preconditions of a method are formed by two things: It’s argument type signature The REQUIRES clause int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz).
Image of page 4
12/15/10 Subtypes Creating 5 5 int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). You weaken the argument type signature by allowing supertypes of specific arguments, or by making some arguments optional . We'll see examples of optional arguments in the semester, but we won’t see many
Image of page 5

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

View Full Document Right Arrow Icon
12/15/10 Subtypes Creating 6 6 The postconditions of a method are formed by two things: It’s return type signature The EFFECTS clause. You strengthen the EFFECTS clause by promising everything you used to, plus extra.
Image of page 6
12/15/10 Subtypes Creating 7 7 int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). We could strengthen the effects clause of Foo::Bar() by printing a message to the screen for each invocation, in addition to computing bar(baz) . Something expecting bar(baz) would not be inconvenienced by that message.
Image of page 7

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

View Full Document Right Arrow Icon
12/15/10 Subtypes Creating 8 8 The final way of creating a subtype is to add some new method to the subtype. Any code expecting only the "old" methods will still see all of them. The "new" method makes no difference. As an example, we'll create a subtype of IntSet , called MaxIntSet , by adding an operation that returns the maximum element in the set.
Image of page 8
12/15/10 Subclasses Creating Subclasses using inheritance 9 9 C++ has a mechanism to enable subtyping, called "subclassing", or sometimes "inheritance". So, if we have some ADT class foo , and want to make a subtype class bar , we do so by saying: class bar : public foo { ...
Image of page 9

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

View Full Document Right Arrow Icon
12/15/10 Subclasses Creating Subclasses using inheritance 10 10 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.
Image of page 10
Image of page 11
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