C++中get set 函数用法,以及运算符重载

1.友元全局函数实现运算符重载

Time.h


class Time {
private:
    int hour;
    int minute;
    int second;
public:
    Time();

    Time(int hour, int minute, int second);

    virtual ~Time();

    int getHour() const;

    void setHour(int hour);

    int getMinute() const;

    void setMinute(int minute);

    int getSecond() const;

    void setSecond(int second);

    void print();
    //成员函数实现运算符重载
    /*Time operator+(Time t) {
        Time newTime;
        newTime.hour = this->hour+ t.hour;
        newTime.minute = this->minute+t.minute;
        newTime.second = this->second+t.second;
        while (newTime.second>=60){
            newTime.second-=60;
            newTime.minute+=1;
        }
        while (newTime.minute>=60) {
            newTime.minute-=60;
            newTime.hour+=1;
        }
        return newTime;
    }*/


};

Time.cpp

//
// Created by Dell on 2021/10/10.
//
#include <iostream>

#include "Time.h"

using namespace std;
Time::Time() {}

Time::Time(int hour, int minute, int second) : hour(hour), minute(minute), second(second) {}

Time::~Time() {
}


int Time::getHour() const {
    return hour;
}

void Time::setHour(int hour) {
    Time::hour = hour;
}

int Time::getMinute() const {
    return minute;
}

void Time::setMinute(int minute) {
    Time::minute = minute;
}

int Time::getSecond() const {
    return second;
}

void Time::setSecond(int second) {
    Time::second = second;
}
void Time::print() {
    cout<<this->hour<<":"<<this->minute<<":"<<this->second<<endl;
}

TestTime.cpp

#include <iostream>
#include "Time.h"

using namespace std;

int main() {
    //友元全局函数运算符重载
    Time operator-(Time t1,Time t2){
        Time newTime;
        newTime.setHour(t1.getHour-t2.getHour);
        newTime.setMinute(t1.getMinute-t2.getMinute);
        newTime.setSecond(t1.getSecond-t2.getSecond);
        return newSecond;
    }

    Time t1;
    t1.setHour(12);
    t1.setMinute(25);
    t1.setSecond(56);
    Time t2(1,2,3);
    t1.print();
    t2.print();
    //Time t3 = t1 + t2;
    Time t3 = t1 - t2;
    t3.print();

    return 0;
}

2.成员函数实现运算符重载

Time1.h


class Time1 {
private:
    int hour;
    int minute;
    int second;
public:
    Time1();

    Time1(int hour, int minute, int second);

    virtual ~Time1();

    int getHour() const;

    void setHour(int hour);

    int getMinute() const;

    void setMinute(int minute);

    int getSecond() const;

    void setSecond(int second);
    void print();
    //成员函数实现运算符重载
    Time1 operator+(Time1 t) {
        Time1 newTime;
        newTime.hour = this->hour+t.hour;
        newTime.minute = this->minute+minute;
        newTime.second = this->second+second;
        while (newTime.second>=60){
            newTime.second-=60;
            newTime.minute+=1;
        }
        while (newTime.minute>=60) {
            newTime.minute-=60;
            newTime.hour+=1;
        }
        return newTime;
    }

};



Time1.cpp

//
// Created by Dell on 2021/10/11.
//
#include <iostream>
#include "Time1.h"

using namespace std;
Time1::Time1() {}

Time1::Time1(int hour, int minute, int second) : hour(hour), minute(minute), second(second) {}

Time1::~Time1() {

}

int Time1::getHour() const {
    return hour;
}

void Time1::setHour(int hour) {
    Time1::hour = hour;
}

int Time1::getMinute() const {
    return minute;
}

void Time1::setMinute(int minute) {
    Time1::minute = minute;
}

int Time1::getSecond() const {
    return second;
}

void Time1::setSecond(int second) {
    Time1::second = second;
}

void Time1::print() {
    cout<<this->hour<<":"<<this->minute<<";"<<this->second<<endl;
}

TestTime1.cpp

#include <iostream>
#include "Time1.h"

using namespace std;
int main(){
    Time1 t1(12,45,12);
    Time1 t2(1,2,3);
    t1.print();
    t2.print();
    Time1 t3 = t1+t2;
    t3.print();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

diary_chao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值