您的位置:首页 > 其它

多态之运算符重载

2016-02-25 21:55 288 查看
1.双目运算符重载

双目运算符重载规则:若要实现表达式oprd1 B oprd2,运算符为B,则需要将B重载为oprd1所属类对象的成员函数,形参为oprd2所属类型

示例代码:复数的加减运算

#include <iostream>
using namespace std;

class Complex {
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator + (const Complex &c) const;
Complex operator - (const Complex &c) const;
void display() const;
private:
double real;
double imag;
};
Complex Complex::operator+(const Complex &c1) const {
return Complex(real+c1.real, imag+c1.imag);
}
Complex Complex::operator-(const Complex &c1) const {
return Complex(real-c1.real, imag-c1.imag);
}
void Complex::display() const {
cout << "(" << real << ", " << imag << ")" << endl;
}
int main() {
Complex c1(4, 5), c2(10, 9), c3;
cout << "c1 = "; c1.display();
cout << "c2 = "; c2.display();
c3 = c1 - c2;
cout << "c3 = c1 - c2 = "; c3.display();
c3 = c1 + c2;
cout << "c3 = c1 + c2 = "; c3.display();
return 0;
}


运行结果:



2.单目运算符重载为类成员函数

(1)前置单目运算符:oprd是A类对象,U是前置运算符,要实现 U oprd,需要将U重载为A类成员函数,没有形参。

(2)后置单目运算符++和--重载规则:若要实现oprd++或者oprd--,则++或者--要被重载为A类成员函数,有一个int形参。

示例代码:时钟的前置++和后置++

#include <iostream>
using namespace std;

class Clock {
public:
Clock(int hours = 0, int minutes = 0, int seconds = 0);
Clock &operator ++ ();
Clock operator ++ (int);
void showTime() const;
private:
int hours, minutes, seconds;
};
Clock::Clock(int hours, int minutes, int seconds) {
if (0 <= seconds && seconds < 60 && 0 <= minutes && minutes < 60 &&
0 <= hours && hours < 24) {
this->hours = hours;
this->minutes = minutes;
this->seconds = seconds;
}
else
cout << "Time error!!" << endl;
}
Clock& Clock::operator ++ () {
seconds++;
if (seconds >= 60) {
seconds -= 60; minutes++;
if (minutes >= 60) {
minutes -= 60; hours = (hours+1)%24;
}
}
return *this;
}
Clock Clock::operator ++ (int) {
Clock old = *this;
++(*this);
return old;
}
void Clock::showTime() const {
cout << hours << ":" << minutes << ":" << seconds << endl;
}
int main() {
Clock myClock(12, 59, 59);
cout << "First showTime: "; myClock.showTime();
cout << "Show myClock++: "; (myClock++).showTime();
cout << "Show ++myClock: "; (++myClock).showTime();
return 0;
}

运行结果:


3.运算符重载为非成员函数

当双目运算符左操作数不是类对象或者不是可以重载该运算符的对象。

(1)重载规则如下:

至少有一个自定义类型参数

后置++和后置--的重载参数,形参列表中要增加一个int,不必写参数名称

当重载运算符需要操作类对象的私有成员时,应当声明为该类对象的友元函数

(2)运算符B重载为非成员函数的形式

双目运算符:原表达式oprd1 B oprd 2, 重载后operator B (oprd1, oprd 2)

前置单目运算符:原表达式B oprd, 重载后operator B (oprd)

后置单目运算符, 原表达式oprd B, 重载后operator B (oprd, 0)

示例代码:复数的+,-和<<,均重载为非成员函数

#include <iostream>
using namespace std;

class Complex {
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
friend Complex operator + (const Complex &c1, const Complex &c2);
friend Complex operator - (const Complex &c1, const Complex &c2);
friend ostream &operator << (ostream &out, const Complex &c);
private:
double real;
double imag;
};
Complex operator + (const Complex &c1, const Complex &c2) {
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
Complex operator - (const Complex &c1, const Complex &c2) {
return Complex(c1.real - c2.real, c1.imag - c2.imag);
}
ostream &operator << (ostream &out, const Complex &c) {
out << "(" << c.real << ", " << c.imag << ")";
return out;
}
int main() {
Complex c1(5, 4), c2(10, 9), c3;
cout << "c1 = " << c1 << endl;
cout << "c2 = " << c2 << endl;
c3 = c1-c2;
cout << "c3 = c1 - c2 = " << c3 << endl;
c3 = c1 + c2;
cout << "c3 = c1 + c2 = " << c3 << endl;
return 0;
}
运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: