将上篇文章中的方法进行修改,看下面头文件的代码
//mytime1.h--Time class before operator overloading
//11.4
#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_H_
//mytime1.cpp -- implementing Time methods
//11.5
#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";
}
if 1
#include <iostream>
#include"mytime1.h"
int main()
{
using std::cout;
using std::endl;
Time planing;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
cout << "Planing time = ";
planing.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;
}
#endif