16+-+Interfaces+and+Invariants

16+-+Interfaces+and+Invariants - EECS 280 Programming and...

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

View Full Document Right Arrow Icon
Interfaces and Invariants EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
ADTs Recall 2 Recall the two main advantages of an ADT: 1. Information hiding : we don't need to know the details of how the object is represented, nor do we need to know how the operations on those objects are implemented. 2. Encapsulation : the objects and their operations are defined in the same place; the ADT combines both data and operation in one entity.
Background image of page 2
ADTs Recall 3 To the caller, an ADT is only an interface. Here are two definitions: Good Definition : The contract for using things of this type. Better Definition : The set of assumptions that each programmer needs to make about another's code in order to demonstrate the correctness of his/her own code. Once you have an interface, you can pick from among many possible
Background image of page 3

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

View Full DocumentRight Arrow Icon
Interfaces Separating out the details 4 The class mechanism, as we've used it so far, has one shortcoming: It co-mingles details of the implementation with the definition of the interface . Recall that the implementation of a class includes: 1. Data members 2. Method implementations
Background image of page 4
Interfaces Separating out the details 5 For example, if we were really building an IntSet , we'd split it across two files: intset.h – the class IntSet {. ..} definition intset.cpp – the implementation of each method Unfortunately, the data members still must be part of the class definition. Since any programmer using an IntSet must see that definition, those
Background image of page 5

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

View Full DocumentRight Arrow Icon
Interfaces Separating out the details 6 Having data objects in the definition has two undesirable effects: 1. It complicates the class definition, making it harder to read and understand. 2. It communicates information to the programmer that s/he isn't supposed to know. The second problem can have very drastic consequences. ***If a programmer using your class
Background image of page 6
Interfaces Separating out the details 7 Question : How can you provide a class definition that carries no implementation details to the client programmer, yet still has interface information? Remember, classes must contain their data members, so this means that the class cannot have an implementation. Answer : Create an "interface-only" class as a base class, from which an implementation can be derived.
Background image of page 7

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

View Full DocumentRight Arrow Icon
Interfaces Creating an abstract base class 8 To create an abstract base class, we first provide an "interface-only" definition of IntSet . This class will
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the 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 / 31

16+-+Interfaces+and+Invariants - EECS 280 Programming and...

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

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