1135人加入学习
(3人评价)
C++编程系列 第三季类设计者的工具

制作于2018.6.26

价格 免费
课程还未发布,不允许加入和购买
#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
class Time
{
private:
	int hours_;
	int minutes_;
public:
	Time(int hours = 0, int minutes = 0);
	~Time();
	void showTime();
	Time operator+(const Time& t);
	Time operator-(const Time& t);
	Time operator*(int const i);
};

int main()
{
	Time t1 = Time(19, 55);
	Time t2 = Time(1, 5);
	Time t3 = Time(5, 30);
	//t1.showTime();
	Time total1 = t1 + t2;
	Time total2 = t1.operator-(t3);
	Time total3 = t3.operator*(2);
	Time total4 = t1 + t3 * 2;
	total1.showTime();
	total2.showTime();
	total3.showTime();
	total4.showTime();
}

Time::Time(int hours, int minutes)
{
	hours_ = hours;
	minutes_ = minutes;
}

Time::~Time() {}

void Time::showTime()
{
	cout << "当前时间为:" << hours_ << "小时" << minutes_ << "分" << endl;
}

Time Time::operator+(const Time& t)
{
	Time Temp;
	Temp.hours_ = this->hours_ + t.hours_;
	if (this->minutes_ + t.minutes_ < 60)
		Temp.minutes_ = this->minutes_ + t.minutes_;
	else
	{
		Temp.hours_ += 1;
		Temp.minutes_ += (this->minutes_ + t.minutes_) % 60;
	}

	return Temp;

}

Time Time::operator-(const Time& t)
{
	Time Temp;
	float i = this->hours_ * 60 + this->minutes_ - t.hours_ * 60 - t.minutes_;
	Temp.hours_ = i / 60;
	Temp.minutes_ = i - Temp.hours_ * 60;
	return Temp;
}

Time Time::operator*(int const i)
{
	int j;
	Time temp;
	j = this->hours_ * i * 60 + this->minutes_ * i;
	temp.hours_ = j / 60;
	temp.minutes_ = j - temp.hours_ * 60;
	return temp;

}

 

[展开全文]

授课教师

SIKI学院老师

课程特色

下载资料(1)
视频(56)