Вы находитесь на странице: 1из 3

Chapter 1

Organizing Computations
C++ offers two ways of organizing your computations: functions and data
structures. Ultimately these can both be combined into a class. We can
start with some information from the first example of Chapter 4 of Accelerated C++ Programming.
1
2
3
4
5
6
7

//We want to start to functionalize our work


// compute a students overall grade from midterm and final
// exam grades and homework grade
double grade(double miderm , double final , double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}

8
9
10
11

// This function above is not the main function , but it is


// specified similarly by setting the
// return type name( the parameter list) { return value ;}

12
13
14

// The parameters behave like parameters that are local to


// function.

15
16
17
18

cout << "Your final grade is " << setprecision (3)


<< grade( midterm , final , sum / count)
<< setprecision (prec) << endl;

19
20
21
22
23

//
//
//
//

Arguements are allowed to expressions such as sum /


count. In this example we are using copies of the
variable to be used in the expression. This is
called "Call by value ".

24
25
26
27

// We will now work with finding a vector double.


// Compute the median of a vector
// we will call by value

CHAPTER 1. ORGANIZING COMPUTATIONS

28
29
30
31
32
33
34

double median(vector <double > vec)


{
typedef vector <double >:: size_type vec_sz;
vec_sz size = vec.size ();
if (size == 0)
throw domain_error ("median of an empty vector");

35

sort(vec.begin (), vec.end());

36
37

vec_sz mid = size /2;

38
39

return size % 2 == 0 ? (vec[mid] + vec[mid -1]) / 2 : vec[mid];

40

41

We define the type vec sz because they are local and we dont want it
to conflict outside this. We dont know who is going to be running our
program, so what we can do is throw an exception if the vector is empty.
When a program throws an exception, execution stops in the part of the
program in which the throw appears, and passes to another part of the
program, along with an exception object. Another point is that because
we are passing by value, we are taking a copy of the original vec vector.
This is important because the sort function changes the values of its input.
Taking the median of a vector should not change the vector itself.
We can combine this grading into a function itself
1
2
3
4
5
6
7
8
9

double grade(double midterm ,


double final ,
const vector <double > & hw) // this line passes
// by reference.
{
if (hw.size () == 0)
throw domain_error ("student has done no homework");
return grade(midterm , final , median(hw));
}

Passing by reference, as we do with the const vectorhdoublei & hw,


means that the reference is another name for the object. hw is another
name for its value, and the const means that we promise not to change the
value. There is no such thing as a reference to a reference.
Another thing to note is that this grade function and the one defined
above have the same name. This is called overloading. A C++ compiler
can tell the difference by the parameter list that we pass to the function.

1.1. CHAPTER 5 IN AC++

We still check for an empty vector because it is more applicable to the


user of the grade function. The other error would not be as useful.

1.0.1

Returning Multiple Values from a Function

There is no direct way to return more than one value from a function, but
you can give the function a reference to an object so it can directly modify
it. This is common for functions that read in inputs.
We have a reference to in and hw so we can modify them! This reference is something that we are not going to copy because it is an lvalue.
Since in the function read hw, we are returning the stream, we can use it
as the argument to an if statement.
Note on clear()
This member function works for both vectors and istream objects. However the function works fundamentally different for both objects. For a
vector, the function clears out the vector for future use. For the istream
object, clear() moves the object out a an error state.

1.0.2

Data structures

A convenient type that can hold information of many different things into
one object is called a structure.

1.1

Chapter 5 in AC++

Вам также может понравиться