13+-+Subtypes+and+Subclasses

# 13+-+Subtypes+and+Subclasses - 5/25/2009 Announcements Ve...

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

5/25/2009 1 Subtypes and Subclasse Ve 280 Programming and Introductory Data Structures Subtypes and Subclasses 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 postconditio 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); 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 it i t t h f h i t i i d d i
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/26/2009 for the course EECS 280 taught by Professor Jeff during the Summer '09 term at University of Michigan-Dearborn.

### Page1 / 8

13+-+Subtypes+and+Subclasses - 5/25/2009 Announcements Ve...

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

View Full Document
Ask a homework question - tutors are online