C++运算符重载小记

C++运算符重载

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

Box operator+(const Box&);

声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:

Box operator+(const Box&, const Box&);

示例代码

class Box
{
public:
	void setLength(double Length)	//设置长度
	{
		length = Length;
	}
	void setBreadth(double Breadth)	//设置宽度
	{
		breadth = Breadth;
	}
	void setHeight(double Height)	//设置高度
	{
		height = Height;
	}
	double getVolume(void)			//计算体积
	{
		return length * breadth * height;
	}
	
	//重载 + 运算符,用于两个Box的相加
	Box operator+(const Box& b)
	{
		Box box;
		box.length = this->length + b.length;
		box.breadth = this->breadth + b.breadth;
		box.height = this->height + b.height;
		return box;
	}
	
	//重载 = 运算符,用比较两个Box是否相等
	bool operator==(const Box& b)
	{
		bool yon;
		if(this->length == b.length && this->breadth == b.breadth && this->height == b.height)
		{
			yon = true;
		}else{
			yon = false;
		}
		return yon;
	}
	
private:
	double length;
	double breadth;
	double height;
};


int main()
{
	Box box0, box1;
	
	//设置box0的属性
	box0.setLength(10.0);
	box0.setBreadth(15.0);
	box0.setHeight(10.0);
	
	//设置box1的属性
	box1.setLength(15.0);
	box1.setBreadth(20.0);
	box1.setHeight(10.0);
	
	//获取box0和box1的体积
	cout<<"The volume of box0: "<<box0.getVolume()<<endl;
	cout<<"The volume of box1: "<<box1.getVolume()<<endl;
	
	//计算box0和box1相加后的体积
	cout<<(box0+box1).getVolume()<<endl;
	
	//判断box0和box1是否相等
	if(box0 == box1)
	{
		cout<<"box0==box1"<<endl;
	}
	else
	{
		cout<<"box0!=box1"<<endl;
	}
	return 0;
}

小结:目前理解为运算符重载主要服务于类对象之间的逻辑运算

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zanerogl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值