Pass by address
That works, but can be a bit cumbersome to use because we have to use the
dereference operator * all over the place in our function body.
Besides that, we have to remember to add the address-of operator whenever we call this function, like:
Moreover, pointers can be tricky and sometimes "dangerous" to use (e.g.,
segmentation faults due to passing an invalid address), and C++ allows us to
avoid some of the situations where we might have to use them.
|
Pass by referenceWe can use a reference as the parameter to the add3 function, in order to address these problems.
Specifying a reference type is similar to specifying a pointer type, except you
use an int& (instead of int*) when specifying a
function parameter.
What that means is that the x passed to the function is a reference to whatever variable the function is called on.
As with passing by address, changing x inside the function also
changes the argument a!
It's quite convenient! We don't need to use deference or address-of operators anymore. |
ostream and istream) by reference.
Compared to using pass-by-address, we can write the same code much simpler
using pass-by-reference.
int readtime(istream&);
...
int readtime(istream& in)
{
int h, m, s;
char c;
in >> h >> c >> m >> c >> s;
return h*3600 + m*60 + s;
}
As you see, the function reads the elapsed amount of time in our
hh:mm:ss format.
With these definitions, I can say
int k = readtime(cin);
when I need to read in a time from the keyboard.
Note: Both cin and all the ifstream objects
can be passed as a reference to istream.
Therefore, I can also say:
ifstream fin("data.txt");
int m = readtime(fin); // pass fin as a reference to istream
... when I need to read in a time from a file data.txt
using the same function readtime.
It would be wonderful to have a function readtime that can take
either cin or fin as an argument, read the elapsed
time in hh:mm:ss format, and return it in seconds.
Although, this is not going to be a major theme for this course, I'd like to
show it to you so that you understand that we really can build new types in C++
if we want to. Also, I should note, this is why + and >> and so on work
with C++ string objects. The implementers of the string library
defined all these operators for their nice string type.
q to quit.
|
First solution. My first solution is a
pretty simple program.
The meat the program is: |
Operator overloading.
While it may be simple, it would be nice to be able to write the function as if
point were a built-in type, meaning that I could
|
p + m means for point objects
p and m. Doing this is quite easy once you
understand the following:
a + b is just the same as the function call
operator+(a,b) in C++.
So if you want to tell the compiler what + means for two
point objects, you need to define the function
operator+(point a,point b) --- i.e. overload the
+ operator for points. The prototype is
clear:
point operator+(point a, point b);
At least I hope it's clear that we should return a point when
we add two points. The function definition is just like any
other function definition:
|
Note on initalizing a struct object: Using the {} clause, variable
S is initalized as follows:
S.x = a.x + b.y; S.y = a.y + b.y;This initializaiton syntax is quite simple and handy! |
istream& operator>>(istream& in, point& A)
{
char c;
return in >> c >> A.x >> c >> A.y >> c;
}
The prototypes of these two should actually make some sense.
For example, we've talked before about how cin >> x
actually evaluates back to cin.
int main()
{
point p, q;
cin >> p >> q;
cout << "midpoint: " << (p+q)/2.0 << endl;
return 0;
}
Sample run:
$ ./a.out
(2.2, 8.8) (-1.1, 1.2)
midpoint: (0.55, 5)