DOC PREVIEW
Yale CPSC 427 - Lecture 14
School name Yale University
Pages 24

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

OutlineDemo: Hangman Game (cont.)Refactored GameCoding Practices RemindersCasts and ConversionsOperator ExtensionsOutline Hangman Coding Casts and Conversions Operator ExtensionsCPSC 427a: Object-Oriented ProgrammingMichael J. FischerLecture 14October 26, 2010CPSC 427a 1/24Outline Hangman Coding Casts and Conversions Operator ExtensionsDemo: Hangman Game (cont.)Refactored GameCoding Practices RemindersCasts and ConversionsOperator ExtensionsCPSC 427a 2/24Outline Hangman Coding Casts and Conversions Operator ExtensionsDemo: Hangman Game (cont.)CPSC 427a 3/24Outline Hangman Coding Casts and Conversions Operator ExtensionsRefactored GameRefactored GameCPSC 427a 4/24Outline Hangman Coding Casts and Conversions Operator ExtensionsRefactored GameRefactored hangman gameDemo 11-Hangman-full extends 10-Hangman in three respects:1. It removes the fixed limitation on the vocabulary size.2. It removes the fixed limitation on the string store size.3. It more clearly separates the model of Board from theviewer/controller.We’ll examine each of these in detail.CPSC 427a 5/24Outline Hangman Coding Casts and Conversions Operator ExtensionsRefactored GameFlex arraysA FlexArray is a growable array of elements of type T.Whenever the array is full, private method grow() is called toincrease the storage allocation.grow() allocates a new array of double the size of the original andcopies the data from the original into it (using memcpy()).Note: After grow(), array is 1/2 full.By doubling the size, the amortized time is O(n) for n items.CPSC 427a 6/24Outline Hangman Coding Casts and Conversions Operator ExtensionsRefactored GameFlex array implementation issuesElement type: A general-purpose FlexArray should allow arraysof arbitrary element type T.If only one type is needed, we can instantiate T using typedef.Example: typedef int T; defines T as synonym for int.C++ templates allow for multiple instantiations.Class types: If T is a class type, then its default constructor anddestructor are called whenever the array grows.They must both be designed so that this does not violate theintended semantics.This problem does not occur with numeric or pointer flexarrays.CPSC 427a 7/24Outline Hangman Coding Casts and Conversions Operator ExtensionsRefactored GameString store limitationCan’t use FlexArray to implement StringStore since pointersto strings would change after grow().Instead, when one StringStore fills up, start another.Only really want another storage pool, not another StringStoreobject.Eacn new Pool is linked to the previous one, enabling all pools tobe deleted by ~StringStore().CPSC 427a 8/24Outline Hangman Coding Casts and Conversions Operator ExtensionsRefactored GameRefactoring Board classOld design for Board contained the board model, the board displayfunctions, and the user-interaction code.New design puts all user interaction into a derived class Player.This makes a clean separation between the model (Board) and thecontroller (Player).The viewer functionality is still distributed between the two.What are the pros and cons of this distribution?CPSC 427a 9/24Outline Hangman Coding Casts and Conversions Operator ExtensionsCoding Practices RemindersCPSC 427a 10/24Outline Hangman Coding Casts and Conversions Operator ExtensionsGet and set functionsWhat’s wrong with the following code?class C {private:int cost;public:int getCost() const { return cost; }void setCost(int n) { cost = n; }...}Rule: Don’t use set functions!(As with all rules, there are sometimes exceptions, but they arerare.)CPSC 427a 11/24Outline Hangman Coding Casts and Conversions Operator ExtensionsCoping with privacy problemsPrivacy violations usually mean that an action is being taken in thewrong class.If a function in class B wants to manipulate variables in class A, itshould delegate the operation to an appropriate function in A.CPSC 427a 12/24Outline Hangman Coding Casts and Conversions Operator ExtensionsType safetyWhat’s wrong with the following code?// void* return to make function genericvoid* pop();Reliable programming comes through checks and balances.Types provide important protections in C++.void* and certain kinds of casts circumvent the type system.Rule: Don’t bypass the type system!CPSC 427a 13/24Outline Hangman Coding Casts and Conversions Operator ExtensionsCasts and ConversionsCPSC 427a 14/24Outline Hangman Coding Casts and Conversions Operator ExtensionsCasts in CA C cast changes an expression of one type into another.Examples:int x;unsigned u;double d;int* p;(double)x; // type double; preserves semantics(int)u; // type unsigned; possible loss of information(unsigned)d; // type unsigned; big loss of information(long int)p; // type long int; violates semantics(double*)p; // preserves pointerness but violates semanticsCPSC 427a 15/24Outline Hangman Coding Casts and Conversions Operator ExtensionsDifferent kinds of castsC uses the same syntax for different kinds of casts.Value casts convert from one representation to another, partiallypreserving semantics. Often called conversions.I(double)x converts integer x to equivalentdouble floating point representation.I(short int)x converts integer x to equivalentshort int, if the integer falls within the rangeof a short int.Pointer casts leave representation alone but change interpretationof pointer.I(double*)p treats bits at destination of p asthe representation of a double.CPSC 427a 16/24Outline Hangman Coding Casts and Conversions Operator ExtensionsC++ castsC++ has four kinds of casts.1. Static cast includes value casts of C. Tries to preservesemantics, but not always safe. Applied at compile time.2. Dynamic cast Applies only to pointers and references toobjects. Preserves semantics. Applied at run time.3. Reinterpret cast is like the C pointer cast. Ignores semantics.Applied at compile time.4. Const cast Allows const restriction to be overridden. Appliedat compile time.CPSC 427a 17/24Outline Hangman Coding Casts and Conversions Operator ExtensionsExplicit cast syntaxC++ supports three syntax patterns for explicit casts.1. C-style: (double)p.2. Functional notation: double(x); myObject(10);.(Note the similarity to a constructor call.)3. Cast notation:int x; myBase* b; const int c;Istatic cast<double>(x);Idynamic cast<myDerived*>(b);Ireinterpret cast<int>(p);Iconst cast<int>(c);CPSC 427a 18/24Outline Hangman Coding Casts and Conversions Operator ExtensionsImplicit castsGeneral rule for implicit casts: If a type A expression appears in acontext where a type B


View Full Document

Yale CPSC 427 - Lecture 14

Download Lecture 14
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 14 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 14 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?