DOC PREVIEW
Duke CPS 006 - From Use to Implementation

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 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 20 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 20 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 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Classes: From Use to ImplementationAnatomy of the Dice classThe header file dice.hAnatomy of a header fileThe header file is a class declarationFrom interface to use, the class DiceHeader file as InterfaceWilliam H. (Bill) Gates, (b. 1955)From Interface to ImplementationImplementation, the .cpp fileMore on method implementationUnderstanding Class ImplementationsClass Implementation HeuristicsBuilding Programs and ClassesDesign HeuristicsDesign Heuristics continuedReference parametersQuadratic Equation ExampleWho supplies memory, where’s copy?ExamplesA Computer Science Tapestry6.1Classes: From Use to ImplementationWe’ve used several classes, a class is a collection of objects sharing similar characteristicsA class is a type in C++, like int, bool, doubleA class encapsulates state and behaviorA class is an object factorystring (this is a standard class), need #include <string>Objects: "hello", "there are no frogs", …Methods: substr(…), length(…), find(…), <<Date need #include "date.h"Objects: December 7, 1949, November 22, 1963Methods: MonthName(), DaysIn(), operator -A Computer Science Tapestry6.2Anatomy of the Dice classThe class Dice, need #include "dice.h"Objects: six-sided dice, 32-sided dice, one-sided diceMethods: Roll(), NumSides(), NumRolls()A Dice object has state and behaviorEach object has its own state, just like each int has its own value•Number of times rolled, number of sidesAll objects in a class share method implementations, but access their own state•How to respond to NumRolls()? Return my own # rollsA Computer Science Tapestry6.3The header file dice.hclass Dice{ public: Dice(int sides); // constructor int Roll(); // return the random roll int NumSides() const; // how many sides int NumRolls() const; // # times this die rolled private: int myRollCount; // # times die rolled int mySides; // # sides on die};The compiler reads this header file to know what’s in a Dice objectEach Dice object has its own mySides and myRollCountA Computer Science Tapestry6.4Anatomy of a header fileA Computer Science Tapestry6.5The header file is a class declarationWhat the class looks like, but now how it does anythingPrivate data are called instance variablesSometimes called data members, each object has its ownPublic functions are called methods, member functions, these are called by client programsThe header file is an interface, not an implementationDescription of behavior, analogy to stereo systemSquare root button, how does it calculate? Do you care?Information hiding and encapsulation, two key ideas in designing programs, object-oriented or otherwiseA Computer Science Tapestry6.6From interface to use, the class Dice#include “dice.h”int main(){ Dice cube(6); Dice dodeca(12); int x = cube.Roll(); int k; for(k=0; k < 6; k++) { x = dodeca.Roll(); } return 0;}Objects constructedcube.myRollCount == 0cube.mySides == 6dodeca.myRollCount == 0dodeca.mySides == 12Method invokedAfter for loopcube.myRollCount == 1cube.mySides == 6dodeca.myRollCount == 6dodeca.mySides == 12A Computer Science Tapestry6.7Header file as InterfaceProvides information to compiler and to programmersCompiler determines how big an object (e.g., Dice cube(6)) is in memoryCompiler determines what methods/member functions can be called for a class/objectProgrammer reads header file (in theory) to determine what methods are available, how to use them, other information about the classWhat about CD, DVD, stereo components?You can use these without knowing how they really workWell-designed and standard interface makes it possible to connect different componentsOO software strives to emulate this conceptA Computer Science Tapestry6.8William H. (Bill) Gates, (b. 1955)CEO of Microsoft, richest person in the world (1999)First developed a BASIC compiler while at HarvardDropped out (asked to leave?) went on to develop Microsoft“You’ve got to be willing to read other people’s code, then write your own, then have other people review your code”Generous to Computer Science and philanthropic in generalVisionary, perhaps cutthroatA Computer Science Tapestry6.9From Interface to ImplementationThe header file provides compiler and programmer with how to use a class, but no information about how the class is implementedImportant separation of concerns, use without complete understanding of implementationImplementation can change and client programs won’t (hopefully) need to be rewritten•If private section changes, client programs will need to recompile•If private section doesn’t change, but implementation does, then client programs relinked, but not recompiledThe implementation of foo.h is typically in foo.cpp, this is a convention, not a rule, but it’s well established (foo.cc used too)A Computer Science Tapestry6.10Implementation, the .cpp fileIn the implementation file we see all member functions written, same idea as functions we’ve seen so farEach function has name, parameter list, and return typeA member function’s name includes its classA constructor is a special member function for initializing an object, constructors have no return typeDice::Dice(int sides)// postcondition: all private fields initialized { myRollCount = 0; mySides = sides;}int Dice::NumSides() const// postcondition: return # of sides of die { return mySides;}A Computer Science Tapestry6.11More on method implementationEach method can access private data members of an object, so same method implementation shared by different objectscube.NumSides() compared to dodeca.NumSides()int Dice::NumSides() const// postcondition: return # of sides of die { return mySides;} int Dice::Roll()// postcondition: number of rolls updated// random 'die' roll returned { RandGen gen; // random number generator (“randgen.h”) myRollCount= myRollCount + 1; // update # of rolls return gen.RandInt(1,mySides); // in range [1..mySides]}A Computer Science Tapestry6.12Understanding Class ImplementationsYou do NOT need to understand implementations to write programs that use classesYou need to understand interfaces, not implementationsHowever, at some point you’ll write your own classesData members are global or accessible to each class methodConstructors should assign values to each instance


View Full Document

Duke CPS 006 - From Use to Implementation

Download From Use to Implementation
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 From Use to Implementation 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 From Use to Implementation 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?