栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.cpp
//
// Created by Dell on 2021/10/10.
//
#include 

#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<hour<<":"<minute<<":"<second< 
TestTime.cpp 
#include 
#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 
#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<hour<<":"<minute<<";"<second< 
TestTime1.cpp 
#include 
#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();

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/316262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号