c++primer第六版十一章第四题

#h文件
#include<iostream>
class time
{
	int minute;
	int hour;
public:
	time() { minute = 0, hour = 0; };
	time(int a, int b) { hour = a, minute = b; };
	time(int a) { hour = a / 60, minute = a - hour * 60; };
    // operator+(time&a);
	friend time operator+(const time&a,const time&b);
	friend time operator-(const time&a, const time&b);
	friend time operator*(const time&a,double b);
	friend time operator*(double b, const time&a) { return a * b; };
	friend std::ostream &operator<<(std::ostream &os, time &a);

	~time();
};

```定义函数
#include "time.h"


time operator+(const time&a, const time&b)
{
	time c;
	int all;
	all = (a.hour * 60 + a.minute + b.hour * 60 + b.minute);
	c.hour =all/60;
	c.minute = all - c.hour * 60;
	return c;
 }
time operator-(const time&a, const time&b)
{
	time c;
	int all;
	all = (a.hour * 60 + a.minute - b.hour * 60 - b.minute);
	c.hour = all / 60;
	c.minute = all - c.hour * 60;
	return c;


 }
 time operator*(const time&a, double b)
{
	 time c;
	 double all;
	 all = (a.hour * 60 + a.minute);
	 all *= b;
	 c.hour = all/ 60;
	 c.minute = all - c.hour * 60;
	 return c;

}
  std::ostream &operator<<(std::ostream &os, time &a)
 {

	  os << "hour:" << a.hour <<"," << "minute:" << a.minute;
	  return os;

 }


time::~time()
{
}

```测试
#include<iostream>
#include"time.h"
using namespace std;
int main()
{
	time test;
	cout << test << endl;
	time test1(2, 10);
	time test10(4,20);
	cout << test1 << endl;
	time test2(100);
	cout << test2 << endl;
	time test3;
	test3 = test1 + 100;
	cout << test3 << endl;
	time test4 = test1 - 200;
	cout << test4 << endl;
	time test5 = test1 - test10;
	cout << test5 << endl;
	time test6 = test1*0.6;
	cout << test6 << endl;
	test6 = 0.5 * test1;
	cout << test6 << endl;





	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值