It’s a simple matter to convert the Time
class to using an overloaded addition operator. You just change the name of Sum()
to the odder-looking name operator+()
. That’s right: You just append the operator symbol (+
, in this case) to the end of operator
and use the result as a method name. This is one place where you can use a character other than a letter, a digit, or an underscore in an identifier name. Listings 11.4 and 11.5 reflect this small change.
// mytime1.h -- Time class before operator overloading
#ifndef MYTIME1_H_
#define MYTIME1_H_
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Time operator+(const Time & t) const;
void Show() const;
};
#endif
// mytime1.cpp -- implementing Time methods
#include <iostream>
#include "mytime1.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m )
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::operator+(const Time & t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Time::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
Like Sum()
, operator+()
is invoked by a Time
object, takes a second Time
object as an argument, and returns a Time
object. Thus, you can invoke the operator+()
method by using the same syntax that Sum()
uses:
total = coding.operator+(fixing); // function notation
But naming the method operator+()
also lets you use operator notation:
total = coding + fixing; // operator notation
Either notation invokes the operator+()
method. Note that with the operator notation, the object to the left of the operator (coding
, in this case) is the invoking object, and the object to the right (fixing
, in this case) is the one passed as an argument. Listing 11.6 illustrates this point.
// usetime1.cpp -- using the second draft of the Time class
// compile usetime1.cpp and mytime1.cpp together
#include <iostream>
#include "mytime1.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
cout << "planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
total = coding + fixing;
// operator notation
cout << "coding + fixing = ";
total.Show();
cout << endl;
Time morefixing(3, 28);
cout << "more fixing time = ";
morefixing.Show();
cout << endl;
total = morefixing.operator+(total);
// function notation
cout << "morefixing.operator+(total) = ";
total.Show();
cout << endl;
return 0;
}
Here is the output of the program in Listings 11.4, 11.5, and 11.6:
planning time = 0 hours, 0 minutes
coding time = 2 hours, 40 minutes
fixing time = 5 hours, 55 minutes
coding + fixing = 8 hours, 35 minutes
more fixing time = 3 hours, 28 minutes
morefixing.operator+(total) = 12 hours, 3 minutes
In short, the name of the operator+()
function allows it to be invoked by using either function notation or operator notation. The compiler uses the operand types to figure out what to do:
int a, b, c;
Time A, B, C;
c = a + b; // use int addition
C = A + B; // use addition as defined for Time objects
Can you add more than two objects? For example, if t1
, t2
, t3
, and t4
are all Time
objects, can you do the following?
t4 = t1 + t2 + t3; // valid?
The way to answer this is to consider how the statement gets translated into function calls. Because addition is a left-to-right operator, the statement is first translated to this:
t4 = t1.operator+(t2 + t3); // valid?
Then the function argument is itself translated to a function call, giving the following:
t4 = t1.operator+(t2.operator+(t3)); // valid? YES
Is this valid? Yes, it is. The function call t2.operator+(t3)
returns a Time
object that represents the sum of t2
and t3
. This object then becomes the object of the t1.operator+()
function call, and that call returns the sum of t1
and the Time
object that represents the sum of t2
and t3
. In short, the final return value is the sum of t1
, t2
, and t3
, just as desired.
18.227.102.50