29
manipulator example #include <iomanip> #include <iostream> 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; }

Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout x endl setprecision(1) x endl

Embed Size (px)

DESCRIPTION

MODULARITY SEPARATE COMPILATION " Functions " Arguments/Parameters " Real Parameters/Formal Parameters " Reference Parameters " Const " Polymorphism clear " sort (begin, end, predicate)

Citation preview

Page 1: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

manipulator example

#include <iomanip>#include <iostream>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; }

Page 2: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

OUTPUT

1.2345711.21.2351.211.23457

Page 3: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

MODULARITYSEPARATE COMPILATION

" Functions

" Arguments/Parameters

" Real Parameters/Formal Parameters

" Reference Parameters

" Const

" Polymorphism clear

" sort (begin, end, predicate)

Page 4: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 5: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

RUN TIME STACK

Page 6: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

EXCEPTION" Stop executing and pass control to an exception

handler

" #include <stdexcept>

" if (size == 0) throw domain_error("local helpful message");

Page 7: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

FUNCTION ARGUMENTS" Arguments (or real parameters) can be

expressions.

" x = a_function(fred, sally, joan+stephen);

Page 8: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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<double> homework;

" vector<double>& hw = homework;

" hw and homework refer to the SAME object.

Page 9: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

REFERENCE (cont.)" Adding const lets us use the value without allowing

us to change the value. (read only)

" vector<double> homework;

" vector<double>& hw = homework;

" const vector<double>& chw = hw;

" All three refer to the SAME object. hw and homework allow read or write, chw allows only read.

Page 10: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 11: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 12: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 13: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 14: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

LVALUE" lvalues are non temporary objects.

" A variable is an lvalue.

" An expression is not an lvalue.

" Don't pass expressions to reference parameters.

Page 15: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

READ FUNCTION EXAMPLEistream& read_hw(istream& in, vector<double>& hw){ if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in;}

Page 16: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

READ FUNCTION EXAMPLEistream& read_hw(istream& in, vector<double>& 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

Page 17: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

READ FUNCTION EXAMPLEistream& read_hw(istream& in, vector<double>& hw){ if(in) { hw.clear(); double x; while (in >> x) hw.push_back(x); in.clear(); } return in;}

BOTH PARAMETERS ARE REFERENCES WHICH MEANSANY CHANGES IN THIS FUNCTIONARE DONE TO THE ARGUMENTIN THE CALLING PROGRAM.

STORES DATA READ BACKIN CALLING PROGRAM'S VECTOR

Page 18: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

READ FUNCTION EXAMPLEistream& read_hw(istream& in, vector<double>& 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 VALUESIN HOMEWORK VECTOR

Page 19: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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)

Page 20: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

THREE PARAMETER TYPES" In the median function, we used a parameter of type

vector<double> 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<double>& to define a reference. The & says don't copy and the const promises the function won't change the original data. (read only)

Page 21: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

THREE PARAMETER TYPES" In the read_hw function we used a parameter of

type vector<double>& to define a reference that says you want to change data in the calling program.

Page 22: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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 PROGRAMBY RETURNING A 1 (return 1;)

Page 23: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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 <Student_info> students;

Page 24: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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;}

Page 25: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 26: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

CATCHING AN EXCEPTION" If you don't catch an exception in a function, it

passes on out to the next calling function.

Page 27: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 28: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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.

Page 29: Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec = cout.precision(); cout  x  endl  setprecision(1)  x  endl

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).