运算符重载c++(完整测试代码)

目录

运算符重载一般格式:

c++中可重载运算符如下:

c++中有下列5个运算符不可重载:

重载运算符使用规则:

运算符重载两种形式:

运算符重载为友元函数:

 运算符重载为成员函数:


运算符重载一般格式:

@表示要重载的运算符;若是重载"="则@用“=”替换;若是重载"+"则@用“+”替换;

参数表根据运算符的操作数个数决定;单目or双目;

c++中可重载运算符如下:

c++中有下列5个运算符不可重载:

重载运算符使用规则:

运算符重载两种形式:

成员运算符函数;友元运算符函数;

对于同一运算符,要么定义为成员,要么定义为友元;不可二者都定义;

运算符重载为友元函数:

友元才能访问类的私有属性;

案例1:友元方式重载运算符“+”、“==”

#include<iostream>
using namespace std;
class Add {
	double real, image;
public:
	Add(double r = 0, double i = 0)
	{
		real = r;
		image = i;
	}
	void show()
	{
		
		if (image > 0)
		{
			if (image == 1)
				cout << real << "+" << "i" << endl;  //虚部系数为1,如:3+i
			else
			{
				cout << real << "+" << image << "i" << endl; //虚部系数为其它正数,如:3+5i
			}
		}
		else if (image < 0) {
			if (image == -1)
				cout << real << "-" << "i" << endl;   //虚部系数为-1情况;如:2-i
			else
			{
				cout << real << "-" << image << "i" << endl; //虚部系数为其它负数,如:3-5i
			}

		}
		else
		{
			cout << real << endl;
		}
	}
	friend Add operator + (const Add& c1, const Add& c2); //类中声明是友元,否则重载运算符的函数不可访问类私有属性

	friend bool operator ==(const Add& c1, const Add& c2);
};
//类外重载运算符"+"
Add operator + (const Add& c1, const Add& c2)  //引用方式传参,保证传参不变加const,返回值数据类型Add
{
	Add temp;
	temp.real = c1.real + c2.real;
	temp.image = c1.image + c2.image;
	return temp;
}
//类外重载运算符"=="
bool operator ==(const Add& c1, const Add& c2) {
	if (c1.real == c2.real && c1.image == c2.image)
		return true;
	else
		return false;
}
int main()
{
	Add a1(10, 5), a2(2, -5);
	a1.show();
	a2.show();
	Add a3 = a1 + a2;   //隐式调用,两个自定义数据类型相加
	// Add a3=operator+(a1,a2);   //显式调用
	a3.show();
	if(a1==a2)
		cout << "两个复数相等" << endl;
	else
	{
		cout << "两个复数不相等" << endl;
	}

	return 0;
}

输出结果:

案例2:定义点类(Point),用以表示几何学点的概念,有属性×、y表示坐标,并重载“-”单目运算符和“=="双目运算符,要求“-”实现对象的成员变量的数值符号取反,而“==”实现判断两个Point类的对象坐标是否相同,采用友元函数的方法实现
例如:
Pointp1(3,4):
Pointp2=-p1;则p2为(-3,-4)
if(p1==p2)表示判断p1和p2坐标是否一样;

#include<iostream>
using namespace std;
class point {
	int x, y;
public:
	point(int xi=0,int yi=0) {
		x = xi;
		y = yi;
	}
	friend bool operator ==(const point& a, const point& b);
	friend point operator -(const point& c);
	void show() {
		cout << "横坐标为:" << x << "    纵坐标为:" << y << endl;
	}
};
bool operator ==(const point&a,const point&b) {
	if (a.x == b.x && a.y == b.y)
		return true;
	else
		return false;
}
point operator -(const point& c) {
	point temp;
	temp.x = -(c.x);
	temp.y = -(c.y);
	return temp;
}
int main() {
	point a(3, 4);
	point b(3, 5);
	if (a == b)
		cout << "a 与 b坐标相同" << endl;
	else 
		cout << "a 与 b坐标不相同" << endl;
	cout << "a坐标如下:" << endl;
	a.show();
	cout << "执行运算a=-b后,a坐标如下:" << endl;
	a = -b;
	a.show();
	return 0;
}

输出结果:

 运算符重载为成员函数:

如果是重载双目运算符,就只要设置一个参数作为右侧运算量,而左侧运算量就是该对象本身;
如果是重载单目运算符,就不必另外设置参数,运算符的操作量就是对象本身。

运算符重载为成员函数与重载为友元函数区别:成元函数方式参数个数少一个(即对象本身);

案例1:定义复数类(Add),采用成员函数实现运算符重载+和==

#include<iostream>
using namespace std;
class Add {
	double real, image;
public:
	Add(double r = 0, double i = 0)
	{
		real = r;
		image = i;
	}
	void show()
	{

		if (image > 0)
		{
			if (image == 1)
				cout << real << "+" << "i" << endl;  //虚部系数为1,如:3+i
			else
			{
				cout << real << "+" << image << "i" << endl; //虚部系数为其它正数,如:3+5i
			}
		}
		else if (image < 0) {
			if (image == -1)
				cout << real << "-" << "i" << endl;   //虚部系数为-1情况;如:2-i
			else
			{
				cout << real << "-" << image << "i" << endl; //虚部系数为其它负数,如:3-5i
			}

		}
		else
		{
			cout << real << endl;
		}
	}
	Add operator + (const Add& c)
	{
		Add temp;
		temp.real = this->real + c.real;
		temp.image = this->image + c.image;
		return temp;
	}
	bool operator ==(const Add& c) {
		if (this->real == c.real && this->image == c.image)
			return true;
		else
			return false;
	}
	
};

int main()
{
	Add a1(10, 5), a2(2, -3);
	a1.show();
	a2.show();
	Add a3 = a1 + a2;  //隐式调用
	// Add a3=a1.operator+(a2);   //显式调用
	a3.show();
	if (a1 == a2)
		cout << "两个复数相等" << endl;
	else
	{
		cout << "两个复数不相等" << endl;
	}

	return 0;
}

输出结果:

visual basic 2005 技术内部中第六章第七节运算符重载代码。 operator部分: Module Module1 Sub Main() End Sub End Module Public Structure Fraction 'Read-Only fields Private num As Long Private den As Long 'Read-Only properties Public ReadOnly Property Numerator() As Long Get Return num End Get End Property Public ReadOnly Property Denominator() As Long Get Return den End Get End Property Sub New(ByVal numerator As Long, ByVal denominator As Long) 'Normalize the numerator and denominator If numerator = 0 Then numerator = 1 ElseIf denominator < 0 Then numerator = -numerator denominator = -denominator End If Dim div As Long = GCD(numerator, denominator) num = numerator \ div den = denominator \ div End Sub 'the greatest common divisor of two numbers (helper method) Private Function GCD(ByVal n1 As Long, ByVal n2 As Long) As Long n1 = Math.Abs(n1) n2 = Math.Abs(n2) Do 'ensure that n1>n2 If n1 < n2 Then Dim tmp As Long = n1 n1 = n2 n2 = tmp End If n1 = n1 Mod n2 Loop While n1 <> 0 End Function 'override ToString to provide a textual representation of the fraction Public Overrides Function ToString() As String If num = 0 OrElse den = 1 Then Return num.ToString Else Return String.Format("{0}/{1}", num, den) End If End Function Public Shared Operator +(ByVal f1 As Fraction, ByVal f2 As Fraction) As Fraction 'a/b+c/d=(a*d+b*c)/(b*d) Return New Fraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den) End Operator Public Shared Operator -(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b-c/d=(a*d-b*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator *(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b * c/d=(a*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator /(ByVal f1 As Fraction, ByVal f2 As Fraction) Return New Fraction(f1.num * f2.den, f1.den * f2.num) End Operator End Structure
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值