The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.

Another Example

Let's convert increment to a member function. Again, we are going to transform one of the parameters into the implicit parameter called this. Then we can go through the function and make all the variable accesses implicit.

void Time::increment (double secs) {
  second += secs;

  while (second >= 60.0) {
    second -= 60.0;
    minute += 1;
  while (minute >= 60) {
    minute -= 60.0;
    hour += 1;

By the way, remember that this is not the most efficient implementation of this function. If you didn't do it back in Chapter 9, you should write a more efficient version now.

To declare the function, we can just copy the first line into the structure definition:

struct Time {
  int hour, minute;
  double second;

  void Time::print ();
  void Time::increment (double secs);

And again, to call it, we have to invoke it on a Time object:

  Time currentTime = { 9, 14, 30.0 };
  currentTime.increment (500.0);
  currentTime.print ();

The output of this program is 9:22:50.

Last Update: 2005-12-05