運算符重載
函數重載保證用戶能夠定義多個名稱相同但是參數列表不同的函數,希望用戶能夠通過同名的函數完成相同的基本操作。
運算符重載允許C++賦予運算符多種含義,而C++允許將運算符重載擴展到用於自定義的類型。例如允許兩個類對象相加+。
時間Time類
類的聲明
//
// Created by Rdw on 2017/3/9.
//
#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
類的定義
//
// Created by Rdw on 2017/3/9.
//
#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
項目代碼
//
// Created by Rdw on 2017/3/9.
//
#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 //PROJECT5_TIME_H
//
// Created by Rdw on 2017/3/9.
//
#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;
}
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