运算符重载

运算符重载的意义

运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据时导致不同的行为。

针对自定义的类,可以对原有运算符进行重载。

运算符重载的规定

C++几乎可以重载全部的运算符,而且只能够重载C++中已经有的。

不能重载的运算符: .         .*         ::          ?:

重载运算符函数可以是类的非静态成员函数(运算符与类的对象的运算相关,关联到每个对象的运算功能,因此是非静态的) 

重载运算符函数可以是类的非成员函数(必要时可以声明为友元)

运算符重载为成员函数

定义形式

函数类型  operator 运算符(形参)

{

...

}

参数个数 = 原操作数个数 - 1  (后置++、--除外)

复数类加减法运算重载为成员函数

#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;//使用重载运算符完成复数减法  编译器看到的是 c1.operator-(c2)
	cout << "c3 = c1 - c2 = "; c3.display();
	c3 = c1 + c2;//使用重载运算符完成复数加法  编译器看到的是 c1.operator+(c2)
	cout << "c3 = c1 + c2 = "; c3.display();
	return 0;
}

 

重载前置++和后置++为时钟类成员函数

前置单目运算符,重载函数没有形参

后置++运算符,重载函数需要有一个int形参

操作数是时钟类的对象

实现时间增加1秒钟

#include <iostream>
using namespace std;

class Clock {
public:
	Clock(int hour = 0, int minute = 0, int second = 0);
	Clock& operator++();
	Clock operator++(int);
	void showTime() const;
private:
	int hour, minute, second;
};
Clock::Clock(int hour, int minute, int second)
{
	if (0 <= hour && hour < 24 && 0 <= minute && minute < 60 && 0 <= second && second < 60)
	{
		this->hour = hour;
		this->minute = minute;
		this->second = second;
	}
	else {
		cout << "Time error!" << endl;
	}
}
void Clock::showTime() const
{
	cout << hour << ":" << minute << ":" << second << endl;
}
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;
}

 

运算符重载为非成员函数

有些运算符不能重载为成员函数,例如二元运算符的左操作数不是对象,或者是不能由我们重载运算符的对象

重载复数的加减法和<<运算符为非成员函数

#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(2, 10), 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;
}

 运算符重载为非成员函数的规则

1.函数的形参代表依次自左至右次序排列的各操作数。

2.重载为非成员函数时,参数个数=原操作数个数(后置++、--除外),至少应该有一个自定义类型的参数。

3.后置单目运算符++和--的重载函数,形参列表中要增加一个int,但不必写形参名。

4.如果在运算符的重载函数中需要操作某类对象的私有成员,可以将此函数声明为该类的友元。

双目运算符B重载后

表达式oprd1 B oprd2  等同于 operator B(oprd1,oprd2)

前置单目运算符B重载后

表达式 B oprd 等同于 operator B(oprd)

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

表达式oprd B 等同于 operator B(oprd,0)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值