C++运算符重载

运算符重载是对已有的运算符赋予多重含义

必要性

C++中预定义的运算符其运算对象只能是基本数据类型,而不适用于用户自定义类型(如类)

实现机制

将指定的运算表达式转化为对运算符函数的调用,运算对象转化为运算符函数的实参。 编译系统对重载运算符的选择,遵循函数重载的选择原则。

可以重载C++中除下列运算符外的所有运算符: .   .*   ::   ?: 只能重载C++语言中已有的运算符,不可臆造新的。 不改变原运算符的优先级和结合性。 不能改变操作数个数。 经重载的运算符,其操作数中至少应该有一个是自定义类型。

两种形式

重载为类的非静态成员函数或重载为非成员函数

运算符函数

声明形式 函数类型  operator 运算符(形参) {        ...... }

重载为类成员函数时   参数个数=原操作数个数-1    (后置++、--除外)

重载为非成员函数时  参数个数=原操作数个数,且至少应该有一个自定义类型的形参。

双目运算符 B :

如果要重载 B 为类成员函数,使之能够实现表达式 oprd1 B oprd2,其中 oprd1 为A 类对象,则 B 应被重载为 A 类的成员函数,形参类型应该是 oprd2 所属的类型。 经重载后,表达式 oprd1 B oprd2 相当于 oprd1.operator B(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 &c2) const;	//运算符+重载成员函数
	Complex operator - (const Complex &c2) const;	//运算符-重载成员函数
	void display() const;	//输出复数
private:	//私有数据成员
	double real;	//复数实部
	double imag;	//复数虚部
};
Complex Complex::operator + (const Complex &c2) const {	//重载运算符函数实现
	return Complex(real + c2.real, imag + c2.imag); //创建一个临时无名对象作为返回值
}

Complex Complex::operator - (const Complex &c2) const {	//重载运算符函数实现
	return Complex(real - c2.real, imag - c2.imag); //创建一个临时无名对象作为返回值
}
void Complex::display() const {
	cout << "(" << real << ", " << imag << ")" << endl;
}
int main() {	//主函数
	Complex c1(5, 4), c2(2, 10), 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;
}

前置单目运算符 U:

如果要重载 U 为类成员函数,使之能够实现表达式 U oprd,其中 oprd 为A类对象,则 U 应被重载为 A 类的成员函数,无形参。 经重载后, 表达式 U oprd 相当于 oprd.operator U()

后置单目运算符 ++和--

如果要重载 ++或--为类成员函数,使之能够实现表达式  oprd++ 或 oprd-- ,其中 oprd 为A类对象,则 ++或--  应被重载为 A 类的成员函数,且具有一个 int 类型形参。 经重载后,表达式  oprd++ 相当于  oprd.operator ++(0)

例:运算符前置++和后置++重载为时钟类的成员函数。 前置单目运算符,重载函数没有形参,对于后置单目运算符,重载函数需要有一个整型形参。 操作数是时钟类的对象。 实现时间增加1秒钟。

#include <iostream>
using namespace std;
class Clock {	//时钟类声明定义
public:	//外部接口
	Clock(int hour = 0, int minute = 0, int second = 0);
	void showTime() const;
	Clock& operator ++ (); //前置单目运算符重载
	Clock operator ++ (int);//后置单目运算符重载
private:	//私有数据成员
	int hour, minute, second;
};
//前置单目运算符重载函数
Clock & Clock::operator ++ () {
	second++;
	if (second >= 60) {
		second -= 60;
		minute++;
		if (minute >= 60) {
			minute -= 60;
			hour = (hour + 1) % 24;
		}
	}
	return *this;
}
//后置单目运算符重载
Clock Clock::operator ++ (int) {
//注意形参表中的整型参数
	Clock old = *this;
	++(*this);	//调用前置“++”运算符
	return old;
}
//其它成员函数的实现略

int main() {
	Clock myClock(23, 59, 59);
	cout << "First time output: ";
	myClock.showTime();
	cout << "Show myClock++:    ";
	(myClock++).showTime();
	cout << "Show ++myClock:    ";
	(++myClock).showTime();
	return 0;
}

运算符非成员函数的设计

函数的形参代表依自左至右次序排列的各操作数。 后置单目运算符 ++和--的重载函数,形参列表中要增加一个int,但不必写形参名。 如果在运算符的重载函数中需要操作某类对象的私有成员,可以将此函数声明为该类的友元。

双目运算符 B重载后, 表达式oprd1 B oprd2 等同于operator B(oprd1,oprd2 ) 前置单目运算符 B重载后, 表达式 B oprd 等同于operator B(oprd ) 后置单目运算符 ++和--重载后, 表达式 oprd B 等同于operator B(oprd,0 )

例:将+、-(双目)重载为非成员函数,并将其声明为复数类的友元,两个操作数都是复数类的常引用。 将<<(双目)重载为非成员函数,并将其声明为复数类的友元,它的左操作数是std::ostream引用,右操作数为复数类的常引用,返回std::ostream引用,用以支持下面形式的输出: cout << a << b; 该输出调用的是: operator << (operator << (cout, a), b);

#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值