Time.h
//
// Time.h
// 运算符重载1-23期92子羽
//
// Created by 墨狂之逸才 on 2018/6/11.
// Copyright © 2018年 墨狂之逸才. All rights reserved.
//
#pragma once
#include<iostream>
using namespace std;
class Time {
public:
Time();
~Time();
Time(int hour,int minute,int second);
Time operator+(const Time &time);
Time operator+=(const Time &time);
Time operator-(const Time &time);
void show();
private:
int m_hour;
int m_minute;
int m_second;
};
Time.cpp
//
// Time.cpp
// 运算符重载1-23期92子羽
//
// Created by 墨狂之逸才 on 2018/6/11.
// Copyright © 2018年 墨狂之逸才. All rights reserved.
//
#include "Time.h"
using namespace std;
Time::Time(){
}
Time::Time(int hour,int minute,int second){
this->m_hour = hour;
this->m_minute = minute;
this->m_second = second;
}
Time::~Time(){
}
//重载为成员函数格式
Time Time::operator+(const Time &time){
int temp = 0;
Time resultTime;
if ((this->m_second + time.m_second) < 60) {
resultTime.m_second = this->m_second + time.m_second;
temp = 0;
} else {
resultTime.m_second = (this->m_second + time.m_second) % 60;
temp = 1;
}
if ((this->m_minute + time.m_minute + temp) < 60) {
resultTime.m_minute =this->m_minute + time.m_minute + temp;
temp = 0;
} else {
resultTime.m_minute = (this->m_minute + time.m_minute + temp) % 60;
temp = 1;
}
if ((this->m_hour + time.m_hour + temp) < 24) {
resultTime.m_hour = this->m_hour + time.m_hour + temp;
temp = 0;
}else {
resultTime.m_hour = (this->m_hour + time.m_hour + temp) % 24;
temp = 1;
}
return resultTime;
}
Time Time::operator+=(const Time &time){
int temp = 0;
if ((this->m_second + time.m_second) < 60) {
this->m_second += time.m_second;
temp = 0;
} else {
this->m_second = (this->m_second + time.m_second) % 60;
temp = 1;
}
if ((this->m_minute + time.m_minute + temp) < 60) {
this->m_minute =this->m_minute + time.m_minute + temp;
temp = 0;
} else {
this->m_minute = (this->m_minute + time.m_minute + temp) % 60;
temp = 1;
}
if ((this->m_hour + time.m_hour + temp) < 24) {
this->m_hour = this->m_hour + time.m_hour + temp;
temp = 0;
}else {
this->m_hour = (this->m_hour + time.m_hour + temp) % 24;
temp = 1;
}
return *this;
}
//重载为成员函数格式
Time Time::operator-(const Time &time){
int temp = 0;
Time resultTime;
if (this->m_second > time.m_second) {
resultTime.m_second = this->m_second - time.m_second;
temp = 0;
} else {
temp = 1;
resultTime.m_second = this->m_second + 60 - time.m_second;//借位
}
if ((this->m_minute - temp - time.m_minute) > 0) {
resultTime.m_minute = this->m_minute - time.m_minute -temp;
temp = 0;
} else {
temp = 1;
resultTime.m_minute = this->m_minute + 60 - time.m_minute;//借位
}
if ((this->m_hour - temp - time.m_hour) > 0) {
resultTime.m_hour = this->m_hour - time.m_hour -temp;
temp = 0;
} else {
resultTime.m_hour = this->m_hour + 24 - time.m_hour - temp;
temp = 1;
}
return resultTime;
}
void Time::show(){
cout << "北京时间:" << m_hour << "时" << m_minute << "分" << m_second << "秒" << endl;
}
main.cpp
//
// main.cpp
// 运算符重载1-23期92子羽
//
// Created by 墨狂之逸才 on 2018/6/11.
// Copyright © 2018年 墨狂之逸才. All rights reserved.
//
#include <iostream>
#include "Time.h"
int main(int argc, const char * argv[]) {
Time t1(17,17,17);
Time t2(0,0,1);
Time t3 = t1 + t2;
t1.show();
t2.show();
t3.show();
cout << "===================" << endl;
t1.show();
t1 += t2;
t1.show();
t2.show();
return 0;
}
运算结果:
北京时间:17时17分17秒
北京时间:0时0分1秒
北京时间:17时17分18秒
===================
北京时间:17时17分17秒
北京时间:17时17分18秒
北京时间:0时0分1秒
Program ended with exit code: 0