DOC PREVIEW
Duke CPS 006 - Topics

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

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

Unformatted text preview:

A Computer Science Tapestry6.1Topicsz Decompositionz Parameter passingz Reading and writing from filesz Designing classesz Reading¾ Chapter 6z Assignment¾ Biorhythms¾ CD ReaderA Computer Science Tapestry6.2Design Heuristicsz Make each function or class you write as single-purpose as possible¾ Avoid functions that do more than one thing, such as reading numbers and calculating an average, standard deviation, maximal number, etc.,• If source of numbers changes how do we do statistics?• If we want only the average, what do we do?¾ Classes should embody one concept, not several. The behavior/methods should be closely relatedz This heuristic is called Cohesion, we want functions and classes to be cohesive, doing one thing rather than several¾ Easier to re-use in multiple contextsA Computer Science Tapestry6.3Design Heuristics continuedz Functions and classes must interact to be useful¾ One function calls another¾ One class uses another, e.g., as the Dice::Roll()function uses the class RandGenz Keep interactions minimal so that classes and functions don’t rely too heavily on each other, we want to be able to change one class or function (to make it more efficient, for example) without changing all the code that uses itz Some coupling is necessary for functions/classes to communicate, but keep coupling loose¾ Change class/function with minimal impactA Computer Science Tapestry6.4Reference parametersz It’s useful for a function to return more than one value¾ Find roots of a quadratic¾ Get first and last name of a userz Functions are limited to one return value¾ Combine multiple return values in object (create a class)¾ Use reference parameters to send values back from function• Values not literally returned• Function call sends in an object that is changed z Sometimes caller wants to supply the object that’s changedstring s = ToLower("HEllO") // return type?string s = "HeLLo";ToLower(s); // return type?A Computer Science Tapestry6.5Quadratic Equation Examplevoid Roots(double a, double b, double c,double& root1, double& root2);// post: root1 and root2 set to roots of// quadratic ax^2 + bx + c// values undefined if no roots exist int main(){double a,b,c,r1,r2;cout << "enter coefficients ";cin >> a >> b >> c;Roots(a,b,c,r1,r2);cout << "roots are " << r1 << " " << r2 << endl;return 0;}A Computer Science Tapestry6.6Who supplies memory, where’s copy?void Roots(double a, double b, double c,double& root1, double& root2);// post: root1 and root2 set to roots of// quadratic ax^2 + bx + c// values undefined if no roots exist z For value parameter, the argument value is copied into memory that “belongs” to parameterz For reference parameter, the argument is the memory, the parameter is an alias for argument memorydouble x, y, w, z;Roots(1.0, 5.0, 6.0, x, y);Roots(1.0, w, z, 2.0, x); // no good, why?A Computer Science Tapestry6.7Examplesz What’s prototype for a function that rolls two N-sided dice Y times and returns the number of double 1’s and double N’s¾ How many input parameters?¾ How many values returned? (reference parameters)¾ Return type of function?¾ Name of function and parameters?void DoubleCount(int sides, int rollCount,int& snakeEyeCount,int& boxCarCount);// pre: sides > 0, rollCount > 0// post: returns # double 1’s (snakeEyeCount)// returns # double N’s N=sides(boxCarCount)A Computer Science Tapestry6.8Another Examplez What’s prototype for a function that returns the number of hours, minutes, and seconds in N seconds?¾ Number of input parameters?¾ Number of output parameters?void SecondsToHours(int& seconds, int& hours,int& minutes);// pre: seconds > 0// post: converts to hours/minutes/seconds, and// returns values in three parameters// Note: seconds is in/out parameterA Computer Science Tapestry6.9Yet another examplez What’s prototype for a function that returns the number of Saturdays and the number of Sundays in a year?¾ How many input parameters?¾ How many output parameters?¾ Name of function and parameters?void SatSunCount(int year,int& saturdayCount,int& sundayCount);//post: returns # saturdays and sundays in year// via corresponding parametersA Computer Science Tapestry6.10Parameter Passing: const-referencez When parameters pass information into a function, but the object passed doesn’t change, it’s ok to pass a copy¾ Pass by value means pass a copy¾ Memory belongs to parameter, argument is copiedz When parameter is altered, information goes out from the function via a parameter, a reference parameter is used¾ No copy is made when passing by reference¾ Memory belongs to argument, parameter is aliasz Sometimes we want to avoid the overhead of making the copy, but we don’t want to allow the argument to be changed (by a malicious function, for example)¾ const-reference parameters avoid copies, but cannot be changed in the functionA Computer Science Tapestry6.11Count # occurrences of “e”z Look at every character in the string, avoid copying the stringint LetterCount(const string& s, const string& letter)// post: return number of occurrences of letter in s{int k, count = 0, len = s.length();for(k=0; k < len; k++){ if (s.substr(k,1) == letter){ count++;}}return count;}z Calls below are legal (but won’t be if just reference parameters)int ec = LetterCount("elephant", "e");string s = "hello"; cout << LetterCount(s, "a");A Computer Science Tapestry6.12General rules for Parametersz Don’t worry too much about efficiency at this stage of learning to program¾ You don’t really know where efficiency bottlenecks are¾ You have time to develop expertisez However, start good habits early in C++ programming ¾ Built-in types: int, double, bool, char, pass by value unless returning/changing in a function¾ All other types, pass by const-reference unless returning/changing in a function¾ When returning/changing, use reference parametersz Const-reference parameters allow constants to be passed, “hello” cannot be passed with reference, but ok const-referenceA Computer Science Tapestry6.13Streams for reading filesz We’ve seen the standard input stream cin, and the standard output streams cout (and cerr)¾ Accessible from <iostream>, used for reading from the keyboard, writing to the screenz Other streams let us read from files and write to files¾ Syntax of reading is the same: a stream is a stream¾ Syntax for writing is the same: a


View Full Document

Duke CPS 006 - Topics

Download Topics
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 Topics 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 Topics 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?