Download presentation
Presentation is loading. Please wait.
Published byJuniper Perkins Modified over 9 years ago
1
manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout << x << endl << setprecision(1) << x << endl << setprecision(2) << x << endl << set precision(3) << x << endl << setprecision(4) << x << endl << setprecision(2) << x << endl << setprecision(1) << x << endl << setprecision(prec) << x << endl; }
2
OUTPUT 1.23457 1 1.2 1.235 1.2 1 1.23457
3
MODULARITY SEPARATE COMPILATION " Functions " Arguments/Parameters " Real Parameters/Formal Parameters " Reference Parameters " Const " Polymorphism clear " sort (begin, end, predicate)
4
FUNCTION PARAMETERS " A communication link between calling and called modules. " In calling program, they are called arguments or actual parameters. " In called program, they are called parameters or formal parameters. " Formal parameters are local to a function. Created when called and destroyed when the function exits.
5
RUN TIME STACK
6
EXCEPTION " Stop executing and pass control to an exception handler " #include " if (size == 0) throw domain_error("local helpful message");
7
FUNCTION ARGUMENTS " Arguments (or real parameters) can be expressions. " x = a_function(fred, sally, joan+stephen);
8
REFERENCE " Allows changing the calling program variable inside a function. " Syntax is to follow formal parameter name with an & " Another name for the same object " vector homework; " vector & hw = homework; " hw and homework refer to the SAME object.
9
REFERENCE (cont.) " Adding const lets us use the value without allowing us to change the value. (read only) " vector homework; " vector & hw = homework; " const vector & chw = hw; " All three refer to the SAME object. hw and homework allow read or write, chw allows only read.
10
OVERLOADING " They do two definitions of the grade function. " This is called overloading. " The compiler can tell the difference because there is a difference in the type of the third parameter.
11
ERROR CHECKING " Checking for errors and throwing exceptions with helpful messages depends on where in the hierarchy you are. Sometimes, the calling program has more information about an error and the called program just needs to detect it.
12
USING REFERENCE " Book mentions returning a value from a function using a reference parameter. " Many people believe this is bad style. " A function should return only one value to the calling program via its name. All other parameters should be in only. " A procedure should be used if you are returning more than one value. None are returned by its name since a procedure is of type void.
13
MORE ON REFERENCE " If you remove the const from the reference parameter, you are saying you expect the function to change the values in the calling program. " Note, you don't put any & on the calling arguments (real parameters) or on the use of the formal parameters inside the function. Only on the definition of the formal parameters in the function statement itself.
14
LVALUE " lvalues are non temporary objects. " A variable is an lvalue. " An expression is not an lvalue. " Don't pass expressions to reference parameters.
15
READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; }
16
READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; } ALLOWS US TO CALL THE FUNCTION IN AN IF AS IN if(read_hw(cin,homework)) AND AN EVEN NEATER USE LATER
17
READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; } BOTH PARAMETERS ARE REFERENCES WHICH MEANS ANY CHANGES IN THIS FUNCTION ARE DONE TO THE ARGUMENT IN THE CALLING PROGRAM. STORES DATA READ BACK IN CALLING PROGRAM'S VECTOR
18
READ FUNCTION EXAMPLE istream& read_hw(istream& in, vector & hw) { if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in; } VERIFYS IT IS O.K. TO TRY A NEW INPUT. CLEARS ANY REASON THAT STOPPED INPUT CLEARS OUT ANY PREVIOUS VALUES IN HOMEWORK VECTOR
19
POLYMORPHIC CLEAR " Previous function shows a good example of polymorphism. " The message clear was sent to two different objects. " These two different objects responded to the message in two different ways. " hw.clear (cleared out the homework vector) " in.clear (cleared out the input stream system)
20
THREE PARAMETER TYPES " In the median function, we used a parameter of type vector to copy the vector into the function. This allowed the function to manipulate the copy without changing the original calling program data. " In the grade function, we used a parameter of type const vector & to define a reference. The & says don't copy and the const promises the function won't change the original data. (read only)
21
THREE PARAMETER TYPES " In the read_hw function we used a parameter of type vector & to define a reference that says you want to change data in the calling program.
22
TRY - CATCH try { // call grade // output statements } catch (domain_error) { // please try again message } TRY STATEMENTS CATCH CLAUSE IF A domain_error OCCURS, IT STOPS THE PROGRAM BY RETURNING A 1 (return 1;)
23
STUDENT STRUCTURE " In the final example, they build a structure for the student name, the mid-term exam score, the final exam score, and the homework vector. (similar to our lab) " Then they define a vector of these structures to store all the student data " vector students;
24
READING STRUCTURES istream& read(istream& is, Student_info& s) { // read and store name and exam grades is >> s.name >> s.midterm >> s.final; // read and store all the student's homework grades read_hw(is, s.homework); return is; }
25
USING CIN WHERE IT IS " Note because of the way they defined read_hw they can just call it to read a set of homework grades from the current position of cin.
26
CATCHING AN EXCEPTION " If you don't catch an exception in a function, it passes on out to the next calling function.
27
SORT " Now to use the sort function, you need to add one more parameter to the sort call. " This third parameter is a predicate (returning true or false) and is used to compare the items in the vector. " For integers or doubles, this can be the default comparison (leave off the third parameter) since the system knows how to compare those.
28
COMPARE bool compare (const Student_info& x, const Student_info& y) { return x.name < y.name; } // uses the string comparison on the name fields of the two structures.
29
DECLARATIONS " same as what we called prototypes in C. " I'll probably continue to call them prototypes. " The last part of chapter 4 gives another discussion of separate compilation, header files, and pre-processor directives (#ifndef, #define, and #endif).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.