首先清单11.10
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include
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;
Time operator-(const Time &t)const;
Time operator*(double n)const;
friend Time operator (double n, const Time &t)
{
return tn;
};
friend std::ostream & operator <<(std::ostream &os, const Time &t);
};
#endif
其次11.11
// stdafx.cpp : 只包括标准包含文件的源文件
//
s
a
f
e
p
r
o
j
e
c
t
n
a
m
e
safeprojectname
safeprojectname.pch 将作为预编译标头
// stdafx.obj 将包含预编译类型信息
#include “stdafx.h”
#include “mytime3.h”
// TODO: 在 STDAFX.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;
}
Time Time::operator-(const Time & t) const
{
Time diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = minutes + 60 * hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time Time::operator*(double mult) const
{
Time result;
long totalminutes = hoursmult * 60 + minutesmult;
result.hours = totalminutes /60;
result.minutes = totalminutes % 60;
return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
os<< t.hours << "hours, " << t.minutes << “minutes”;
return os;
}
最后11.12
// stdafx.cpp : 只包括标准包含文件的源文件
//
s
a
f
e
p
r
o
j
e
c
t
n
a
m
e
safeprojectname
safeprojectname.pch 将作为预编译标头
// stdafx.obj 将包含预编译类型信息
#include “stdafx.h”
#include “mytime3.h”
// TODO: 在 STDAFX.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;
}
Time Time::operator-(const Time & t) const
{
Time diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = minutes + 60 * hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time Time::operator*(double mult) const
{
Time result;
long totalminutes = hoursmult * 60 + minutesmult;
result.hours = totalminutes /60;
result.minutes = totalminutes % 60;
return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
os<< t.hours << "hours, " << t.minutes << “minutes”;
return os;
}
结果 图示
今天打的时候不小心把重载*第一个&漏了,然后报错
图示
和
运行结果
开始很奇怪,就是为什么要引用,return,不是会返回一个副本的吗,拷贝的对象不也一样可以用吗,反正也不是值,就是一个输出流对象拷贝就拷贝,开始是这么想的,但是当我查了一些资料后,别人说,输入流的对象是无法被拷贝的,应该没有对应的复制构造函数,同理,输出流对象也是如此,不然你看我试的,如果他有对应的复制构造函数,那早就复制了。可惜他没有。而一旦返回非引用就要拷贝,所以是显然无法编译通过的,我也是第一次知道,输入流输出流对象没有复制构造函数,