/************************************************************************/
/*
创建友元:
1)将其原型放在类声明中,并在原性声明前加上关键字friend;
friend Time operator* (double m, const Time & t);
注:a)虽然operator * ()函数是在类声明的,但他不是成员函数,因此不能使用成员操作符来调用;
b)虽然operator * ()函数不是成员函数,但它与成员函数的访问权限相同;
2)编写函数定义:因为他不是成员函数,所以不要使用Time::限定符。两外,不要在定义中使用关键字friend;
*/
/************************************************************************/
#ifndef MYTIME0_H_
#define MYTIME0_H_
class Time
{
private:
int hours;
int miuntes;
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 Sum (const Time & t) const;
//将Time类转化为重载的加法操作符:只要将Sum()的名称改为operator+()即可;
Time operator+ (const Time & t) const;
// 其他操作符
Time operator- (const Time & t) const;
Time operator* (double n) const;
void Show() const;
};
#endif
/**
*/
#include <iostream>
#include "mytime0.h"
Time::Time()
{
hours = miuntes = 0;
}
Time::Time(int h, int m)
{
hours = h;
miuntes = m;
}
// 当总的分钟数超过59时,使用整数除法和求余运算来调整minutes和hours的值;
void Time::AddMin(int m)
{
miuntes += m;
hours += miuntes / 60;
miuntes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
// 调整和重新设置时间
void Time::Reset(int h, int m)
{
hours = h;
miuntes = m;
}
/************************************************************************/
/*
sum()中参数是引用,但返回类型却不是引用这样做的好处:
将参数声明为引用是为了提高效率,不要返回指向局部变量或临时对象的引用,
函数执行完毕后,局部变量和临时对象将消失,引用将指向不存在的数据;
*/
/************************************************************************/
// 将两个时间求和
// 将Time类转化为重载的加法操作符
/*Time Time::Sum(const Time & t) const
{
Time sum;
sum.miuntes = miuntes + t.miuntes;
sum.hours = hours + t.hours + sum.miuntes / 60;
sum.miuntes %= 60;
return sum;
}*/
// 加法操作符重载
Time Time::operator +(const Time & t) const
{
Time sum;
sum.miuntes = miuntes + t.miuntes;
sum.hours = hours + t.hours + sum.miuntes / 60;
sum.miuntes %= 60;
return sum;
}
// 减法操作符重载
Time Time::operator -(const Time & t) const
{
Time diff;
int tot1, tot2;
tot1 = t.miuntes + 60 * t.hours;
tot2 = miuntes + 60 * hours;
diff.miuntes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
// 乘法操作符重载
Time Time::operator *(double mult) const
{
Time result;
long totalminutes = hours * mult * 60 + miuntes * mult;
result.hours = totalminutes / 60;
result.miuntes = totalminutes % 60;
return result;
}
// 显示时间
void Time::Show() const
{
std::cout << hours << " hours. " << miuntes << " minutes";
}
/************************************************************************/
/* */
/************************************************************************/
#include <iostream>
#include "mytime0.h"
int main()
{
// 使用std::cout 比导入整个名称空间更经济
using std::cout;
using std::endl;
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
Time diff;
Time adjusted;
cout << "Planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
/*total = coding.Sum(fixing);
cout << "coding.Sum(fixing) = ";
total.Show();
cout << endl;*/
/************************************************************************/
/*
调用operator+()调用的两种方式:
1)像调用Sum()那样:
total = coding.operator + (fixing);
2)在操作符表示法中,操作符左侧的对象(coding)是调用对象,操作符右边的对象(fixing)
是作为参数被传递的对象;
total = coding + fixing;
*/
/************************************************************************/
total = coding + fixing;
cout << "coding + fixing = ";
total.Show();
cout << endl;
Time morefixing(3, 28);
cout << "more fixing time = ";
morefixing.Show();
cout << endl;
// operator+()函数的名称使得可以使用函数表示或操作符表示来调用它;
total = morefixing.operator +(total);
cout << "morefixing.operator+(total) = ";
total.Show();
cout << endl;
return 0;
}