{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

13+-+Subtypes+and+Subclasses

# 13+-+Subtypes+and+Subclasses - Announcements Ve 280...

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

5/25/2009 1 Subtypes and Subclasses Ve 280 Programming and Introductory Data Structures 1 Announcements struct Outcome { bool canMove; // true if a move exists, false if forfiet int pawnNum; // Which of the player's pawns to move Pawn oppononet ;// Which opponent is victim of swap/sorry }; should be: 2 struct Outcome { bool canMove; // true if a move exists, false if forfiet int pawnNum; // Which of the player's pawns to move Pawn opponent ; // Which opponent is victim of swap/sorry }; This affects project3.pdf, p3.h, p3t2.cpp, and your .cpp file. I will fix the first three once Sakai is back up… Subtypes Recall – Introduction y Suppose we have two types, S and T. y S is a subtype of T, written “S <: T”, if: y 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> y Objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program (correctness). y This is called the “substitution principle”. y If S <: T, then we also say that “T is a supertype of S”. 3 Subtypes Creating y 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 4 2. Strengthen the postcondition of one or more operations. 3. Add one or more operations. Subtypes Creating y The preconditions of a method are formed by two things: y It’s argument type signature y The REQUIRES clause int Foo::Bar(int baz); // Q S b i i i d 5 // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). y You could weaken this REQUIRES clause by allowing: y Negative, even integers y Positive integers y All integers Subtypes Creating int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). Y k th t t i t b ll i 6 y You weaken the argument type signature by allowing supertypes of specific arguments, or by making some arguments optional . y We'll see examples of optional arguments in the semester, but we won’t see many instances of weakening through supertypes.

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

View Full Document
5/25/2009 2 Subtypes Creating y The postconditions of a method are formed by two things: y It’s return type signature y The EFFECTS clause. y You strengthen the EFFECTS clause by promising everything 7 you used to, plus extra. Subtypes Creating int Foo::Bar(int baz); // REQUIRES: baz is positive and even. // EFFECTS: returns bar(baz). y We could strengthen the effects clause of Foo::Bar() by i ti t th f h i ti i dditi t 8 printing a message to the screen for each invocation, in addition to computing bar(baz) . y Something expecting bar(baz) would not be inconvenienced by that message. y You strengthen the return type by promising to return a subtype instead. We won't see many instances of strengthened return types in this course.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}