Manipulator example #include int main (void) { double x = 1.2345678; streamsize prec =...

Preview:

DESCRIPTION

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

Citation preview

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

OUTPUT

1.2345711.21.2351.211.23457

MODULARITYSEPARATE COMPILATION

" Functions

" Arguments/Parameters

" Real Parameters/Formal Parameters

" Reference Parameters

" Const

" Polymorphism clear

" sort (begin, end, predicate)

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.

RUN TIME STACK

EXCEPTION" Stop executing and pass control to an exception

handler

" #include <stdexcept>

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

FUNCTION ARGUMENTS" Arguments (or real parameters) can be

expressions.

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

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.

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.

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.

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.

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.

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.

LVALUE" lvalues are non temporary objects.

" A variable is an lvalue.

" An expression is not an lvalue.

" Don't pass expressions to reference parameters.

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

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

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

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

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)

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)

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.

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

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;

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

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.

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

passes on out to the next calling function.

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.

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.

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

Recommended