运算符重载
函数重载保证用户能够定义多个名称相同但是参数列表不同的函数,希望用户能够通过同名的函数完成相同的基本操作。
运算符重载允许C++赋予运算符多种含义,而C++允许将运算符重载扩展到用于自定义的类型。例如允许两个类对象相加+。
时间Time类
类的声明
#ifndef PROJECT5_TIME_H
#define PROJECT5_TIME_H
class Time {
private:
int hour;
int minute;
public:
Time();
Time(
int h ,
int m);
~Time();
void show()
const;
void reset(
int h ,
int m);
};
#endif //PROJECT5_TIME_H
类的定义
#include "Time.h"
#include <iostream>
using namespace std;
Time::Time() {
}
Time::Time(
int h,
int m) {
hour = h;
minute = m;
}
Time::~Time() {
}
void Time::reset(
int h,
int m) {
hour = h;
minute = m;
}
void Time::show()
const {
cout << hour <<
"hours " << minute <<
"minutes" << endl;
}
类的使用
#include <iostream>
#include "Time.h"
using namespace
std
int main() {
Time time1 = Time(
14 ,
46)
time1
.show()
Time time2(
8 ,
30)
time2
.show()
time1
.reset(
7 ,
0)
time1
.show()
}
测试结果
E:
\Project5
\cmake-build-debug
\Project5.exe
14hours 46minutes
8hours 30minutes
7hours 0minutes
Process finished with exit code 0
算术运算符重载
算术运算符包括一元正号(+)、一元负号(-)、乘法(*)、除法(/),求余(%),加法(+),减法(-)。
运算符函数的声明
friend
Time operator+(
const Time &object1 ,
const Time &object2);
运算符函数的定义
Time operator+(
const Time &object1,
const Time &object2) {
Time sum;
int temp;
temp = (object1.
hour *
60 + object1.
minute) + (object2.
hour *
60 + object2.
minute);
sum.
hour = temp /
60;
sum.
minute = temp %
60;
return sum;
}
运算符函数的使用
#include <iostream>
#include "Time.h"
using namespace
std
int main() {
Time time1 = Time(
14 ,
46)
time1
.show()
Time time2(
8 ,
30)
time2
.show()
time1
.reset(
7 ,
0)
time1
.show()
Time time3
time3 = time1 + time2
time3
.show()
}
测试结果
E:
\Project5
\cmake-build-debug
\Project5.exe
14hours 46minutes
8hours 30minutes
7hours 0minutes
15hours 30minutes
Process finished with exit code 0
项目代码
#ifndef PROJECT5_TIME_H
#define PROJECT5_TIME_H
class Time {
private:
int hour;
int minute;
public:
Time();
Time(
int h ,
int m);
~Time();
void show()
const;
void reset(
int h ,
int m);
friend Time
operator+(
const Time &object1 ,
const Time &object2);
friend Time
operator-(
const Time &object1 ,
const Time &object2);
friend Time
operator*(
const Time &object1 ,
const Time &object2);
friend Time
operator/(
const Time &object1 ,
const Time &object2);
};
#endif
//
// Created by Rdw on
2017/
3/
9.
//
using namespace std;
Time::Time() {
}
Time::Time(
int h,
int m) {
hour = h;
minute =
m;
}
Time::~Time() {
}
void Time::
reset(
int h,
int m) {
hour = h;
minute =
m;
}
void Time::show() const {
cout << hour <<
"hours " << minute <<
"minutes" << endl;
}
Time operator+(const Time &object1, const Time &object2) {
Time sum;
int temp;
temp = (object1.hour *
60 + object1.minute) + (object2.hour *
60 + object2.minute);
sum.hour = temp /
60;
sum.minute = temp %
60;
return sum;
}
Time operator-(const Time &object1, const Time &object2) {
Time
sub;
int temp;
temp = (object1.hour *
60 + object1.minute) - (object2.hour *
60 + object2.minute);
sub.hour = temp / 60;
sub.minute = temp % 60;
return sub;
}
Time operator
*(const Time &object1, const Time &object2) {
Time mult;
int temp;
temp = (object1.hour *
60 + object1.minute) * (object2.hour *
60 + object2.minute);
mult.hour = temp /
60;
mult.minute = temp %
60;
return mult;
}
Time operator/(const Time &object1, const Time &object2) {
Time div;
int temp;
temp = (object1.hour *
60 + object1.minute) / (object2.hour *
60 + object2.minute);
div.hour = temp /
60;
div.minute = temp %
60;
return div;
}
#include <iostream>
#include "Time.h"
using namespace
std
int main() {
Time time1 = Time(
14 ,
46)
time1
.show()
Time time2(
8 ,
30)
time2
.show()
time1
.reset(
7 ,
0)
time1
.show()
Time time3
time3 = time1 + time2
time3
.show()
Time time4 = time3 - time2
time4
.show()
Time time5 = time1 * time2
time5
.show()
Time time6 = time5 / time1
time6
.show()
}
E:
\Project5
\cmake-build-debug
\Project5.exe
14hours 46minutes
8hours 30minutes
7hours 0minutes
15hours 30minutes
7hours 0minutes
3570hours 0minutes
8hours 30minutes
Process finished with exit code 0
转载请注明原文地址: https://ju.6miu.com/read-12776.html