运算符重载的格式如下:
operator op(argument-list)
其中op必须是有效的C++运算符,不能虚构一个新的符号
mytime0.h
#pragma once
class Time
{
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Time Sum(const Time & t) const; //const 加在函数的后边表示不会改变传入的参数
void Show() const;
private:
int hours;
int minutes;
};
mytime0.cpp
#include <iostream>
#include "mytime0.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::Sum(const Time & t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Time::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
usetime0.cpp
#include <iostream>
#include "mytime0.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
cout << "planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
total = coding.Sum(fixing);
cout << "coding.Sum(fixing) = ";
total.Show();
cout << endl;
system("pause");
return 0;
}
运行结果图
添加加法运算符
将Time类转换为重载的加法运算符很容易,只要将Sum()的名称改为operator +()即可。
mytime1.h
#pragma once
class Time
{
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Time operator+(const Time & t) const; //const 加在函数的后边表示不会改变传入的参数
void Show() const;
private:
int hours;
int minutes;
};
mytime1.cpp
#include <iostream>
#include "mytime0.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::operator+(const Time & t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Time::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
usetime1.cpp
#include <iostream>
#include "mytime0.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
cout << "planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
total = coding + fixing;
cout << "coding + fixing = ";
total.Show();
cout << endl;
Time morefixing(3, 28);
cout << "more fixing time = ";
morefixing.Show();
cout << endl;
total = morefixing.operator+(total);
cout << "morefixing.operator+(total) = ";
total.Show();
cout << endl;
system("pause");
return 0;
}
运行结果图
总之,operator+()函数的名称可以是的可以使用函数表示法或运算符表示法来调用它,编译器将根据操作数的类型来确定如何做:
int a, b, c;
Time A, B, C;
c = a + b; //use int additon
C = A + B; //use addition as defined for Time objects
不能重载下面的运算符:
- sizeof sizeof运算符
- . 成员运算符
- .* 成员指针运算符
- :: 作用域解析运算符
- ?: 条件运算符
- typeid 一个RTTI运算符
- const_cast 强制类型转换运算符
- dynamic_cast 强制类型转换运算符
- reinterpret_cast 强制类型转换运算符
- static_cast 强制类型转换运算符
可重载的运算符
+ | - | * | / | % | ^ |
---|---|---|---|---|---|
& | | | ~= | ! | = | < |
> | += | -= | *= | /= | %= |
^= | &= | |= | << | >> | >>= |
<<= | == | != | <= | >= | && |
|| | ++ | – | , | ->* | -> |
() | [] | new | delete | new[] | delete[] |
上表中的大多数运算符都可以通过成员或非成员函数进行重载,但下面的运算符只能通过成员函数来进行重载:
- = 赋值运算符
- () 函数调用运算符
- [] 下标运算符
- -> 通过指针访问类成员的运算符
其他重载运算符
还有一些其他的操作对Time类来说是有意义的。例如,可能要将两个时间相减或将时间乘以一个因子,这需要重载减法和乘法运算符。这和重载加法运算符采用的技术相同,即创建operator-()和operator*()方法。也就是说,将下面的原型添加到类的声明中:
Time operator-(const Time & t) const;
Time operator*(double n) const;
mytime2.h
#pragma once
class Time
{
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Time operator+(const Time & t) const; //const 加在函数的后边表示不会改变传入的参数
Time operator-(const Time & t) const;
Time operator*(double n) const;
void Show() const;
private:
int hours;
int minutes;
};
mytime2.cpp
#include <iostream>
#include "mytime0.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::operator+(const Time & t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time Time::operator-(const Time & t)const
{
Time sub;
sub.minutes = minutes - t.minutes;
if (sub.minutes < 0)
{
sub.minutes += 60;
sub.hours = hours - t.hours - 1;
}
else
{
sub.hours = hours - t.hours;
}
return sub;
}
Time Time::operator*(double n) const
{
Time result;
long totalminutes = hours * n * 60 + minutes * n;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
void Time::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
usetime2.cpp
#include <iostream>
#include "mytime0.h"
int main()
{
using std::cout;
using std::endl;
Time weeding(4, 35);
Time waxing(2, 47);
Time total;
Time diff;
Time adjusted;
cout << "weeding time = ";
weeding.Show();
cout << endl;
cout << "waxing time = ";
waxing.Show();
cout << endl;
cout << "total work time = ";
total = weeding + waxing;
total.Show();
cout << endl;
diff = weeding - waxing;
cout << "weeding time - waxing time = ";
diff.Show();
cout << endl;
adjusted = total * 1.5;
cout << "adjusted work time = ";
adjusted.Show();
cout << endl;
system("pause");
return 0;
}