C++primer plus第六版课后编程题答案11.4

mytime.h


#ifndef MYTIME_h_
#define MYTIME_h_
#include 
//为了方便我全部写成inline函数了
class Time
{
private:
	int hours;
	int minutes;
public:
	Time(){
		hours=minutes=0;
	};
	Time(int h,int m=0){
		hours=h;
		minutes=m;
	};
	void Reset(int h=0,int m=0)
	{
		hours=h;
		minutes=m;
	};
	Time operator+(const Time &t)const
	{
		int jinwei=0;
		int newh=hours+t.hours;
		int newm=minutes+t.minutes;
		if(newm>60)
		{
			newm=newm%60;
			jinwei=1;
		}
		newh+=jinwei;
		return Time(newh,newm);
	};

	Time operator+(double m)const//重载+
	{
		int jinwei=0;
		int newm=m+minutes;
		if(newm>60)
		{
			newm=newm%60;
			jinwei=1;
		}
		int newh=hours+jinwei;
		return Time(newh,newm);
	};


	Time operator-(const Time &t)const
	{
		int jinwei=0;
		int newh=hours-t.hours;
		int newm=minutes-t.minutes;
		if(newm<0)
		{
			newm=minutes-t.minutes+60;
			jinwei=-1;
		}
		newh+=jinwei;
		return Time(newh,newm);	
	
	
	};
	Time operator*(double n)const{
		double totalsource=hours*60+minutes;//原来的分钟数
		double totalnow=totalsource*n;
		int newh=totalnow/60;
		int newm=int(totalnow)%60;//求余必须为int
		return Time(newh,newm);
	};
	friend Time operator*(double n,const Time &t){
		return t*n;
	};
	friend Time operator-(const Time &t,double m){
		double total=t.hours*60+t.minutes;
		int now=total-m;
		int newh=now/60;
		int newm=now%60;//求余必须为int
		return Time(newh,newm);
	};
	friend Time operator+(double m,const Time &t)
	{
		return t+m;
		
	}
	friend std::ostream &operator<<(std::ostream &os,const Time &t)
	{
		os<

main114.cpp

#include
#include"myTime.h"
using namespace std;
void main114()
{
	Time t1;
	Time t2(6,9);
	Time t3(7);
	Time t4(4,59);
	/*构造测试
	cout<


你可能感兴趣的:(C++,primer,plus第六版编程练习,c++,primer,plus学习)