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();
}
|