美文网首页
一个时钟类,成员变量为时分秒。 重载 + += - 三个

一个时钟类,成员变量为时分秒。 重载 + += - 三个

作者: 墨狂之逸才 | 来源:发表于2018-06-11 19:11 被阅读21次

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

相关文章

  • 一个时钟类,成员变量为时分秒。 重载 + += - 三个

    Time.h Time.cpp main.cpp 运算结果:

  • static 关键字

    修饰类的成员变量和成员方法 静态成员为类的实例所共享 静态方法不能重载,为静态 修饰类 只能修饰内部类。有三个特点...

  • JAVA之重载

    封装:用类定义成员变量,并把操作成员变量的代码都放在类里面。 1、重载 方法签名:方法名+依次参数类型。方法签名是...

  • 7、静态方法

    我们知道一个类中有成员变量和静态变量,那既然有成员的方法,应该也有静态的方法。 同样的成员方法有重载方法,那么静态...

  • final关键字

    final关键字的作用: 修饰类,类不能被继承 修饰成员方法,方法不能被重载(override) 修饰成员变量,变...

  • Java 2 对象交互

    2.1 对象交互 2.1.1 时钟的设计: 用一个类来表示时钟和分钟。对象Display:(属性)成员变量valu...

  • Java基础6-多态;匿名内部类;适配器模式

    昨日内容回顾 类成员构造函数:和类同名,没有返回值,可以重载this(),super()成员变量:成员函数:静态代...

  • Flutter之Dart语言 面向对象(一)

    目录 实例化成员变量 构造函数 读取和写入对象 setters 和 Getters 重载操作 继承类 抽象类 对象...

  • BaseBuilder源码

    首先这是个抽象类。成员变量: 构造函数: 这个很有趣,这个类有三个成员变量。但是只传入一个参数,另外两个成员变量是...

  • 学习笔记第一周(Boolan或博览网)

    一、构造函数1、与类同名2、可以在构造函数中给成员成员变量直接赋值 二、运算符重载 重载成员函数,隐藏一个参数,该...

网友评论

      本文标题:一个时钟类,成员变量为时分秒。 重载 + += - 三个

      本文链接:https://www.haomeiwen.com/subject/ywoheftx.html