15+-+Subtypes+and+Subclasses

15+-+Subtypes+and+Subclasses - Subtypes and Subclasses EECS...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Subtypes and Subclasses EECS 280 Programming and Introductory Data Structures 1 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). This is called the “substitution principle”. 2 Subtypes Creating 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. Subtypes Creating 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). Subtypes Creating 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 instances of weakening through Subtypes Creating 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. Subtypes Creating 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. You strengthen the return type by Subtypes Creating 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. Subclasses Creating Subclasses using inheritance 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 { ... }; Subclasses Creating Subclasses using inheritance 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....
View Full Document

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

Page1 / 42

15+-+Subtypes+and+Subclasses - Subtypes and Subclasses EECS...

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

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