双目运算符重载及友元函数重载

#include <iostream>
using namespace std;
class CComplex
{
private:
	double real,virt;
public:
	CComplex(double r=0,double i=0)
	{
		real=r;
		virt=i;
	}

	CComplex operator+(CComplex num);
	CComplex operator-(CComplex num);
	CComplex operator*(CComplex num);
	void operator()();
};

CComplex CComplex::operator+(CComplex num)
{
	CComplex temp;
	temp.real=real+num.real;
	temp.virt=virt+num.virt;
	return temp;
}

CComplex CComplex::operator-(CComplex num)
{
	CComplex temp;
	temp.real=real-num.real;
	temp.virt=virt-num.virt;
	return temp;
}

CComplex CComplex::operator*(CComplex num)
{
	CComplex temp;
	temp.real=real*num.real-virt*num.virt;
	temp.virt=real*num.virt+virt*num.real;
	return temp;
}

void CComplex::operator()()
{
	cout << real;
	if(virt>0) cout << "+" << virt << "i" << endl;
	if(virt<0) cout << virt << "i" << endl;
}

int main()
{
	CComplex com1(1,2),com2(3,4),totall1,totall2,totall3;
	totall1=com1+com2;// totall1=com1.operator+(com2);
	totall2=com1-com2;// totall2=com1.operator-(com2);
	totall3=com1*com2;// totall3=com1.operator*(com2);

	cout << "输出复数com1和com2" << endl;
	cout << "com1=" ;
	com1();
	cout << "com2=";
	com2();
	cout << endl << "---复数运算---" << endl;

	cout << "com1+com2=";
	totall1();
	cout << endl;
	
	cout << "com1-com2=";
	totall2();
	cout << endl;


	cout << "com1*com2=";
	totall3();
	cout << endl;


}


一定不要忘记 using namespace std;

要是忘了,就会出现“cout”: 未声明的标识符。、

现在还不明白的问题: 现在的+  -  * ()都已经重载了。当运算com1+com2时候,把+看成一个函数,后面是com2,所以把com2(3,4)传递到类num。

num.real=3, num.virt=4.为什么real=1.virt=2?


友元单目运算:

#include<iostream>
using namespace std;

class CTri
{
	int a,b;
public:
	CTri(int x=0,int y=0)
	{
		a=x;
		b=y;
	}
	friend CTri operator -(CTri obj);
	void show();
};

CTri operator - (CTri obj)
{
	obj.a=-obj.a;
	obj.b=-obj.b;
	return obj;	
}

void CTri::show()
{
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
} 


void main()
{
	CTri t1(2,15),t2;
	cout << "输出对象t1的数据成员:" << endl;
	t1.show();
	cout << "输出对象t2的数据成员:" << endl;
	t2.show();
	t2=-t1;
	cout << "输出对象t1取反后的数据成员:" << endl;
	t2.show();
}


#include<iostream>
using namespace std;

class CComplex
{
private:
	double real,virt;
public:
	CComplex(double r=0,double i=0 )
	{
		real=r;
		virt=i;
	}

	friend CComplex operator +(CComplex num1, CComplex num2);
	friend CComplex operator -(CComplex num1, CComplex num2);
	friend CComplex operator *(CComplex num1, CComplex num2);
	void operator()();
};


CComplex operator +(CComplex num1,CComplex num2)
{
	CComplex temp;
	temp.real=num1.real+num2.real;
	temp.virt=num1.virt+num2.virt;
	return temp;
}

CComplex operator-(CComplex num1,CComplex num2)
{
	CComplex temp;
	temp.real=num1.real-num2.real;
	temp.virt=num1.virt-num2.virt;
	return temp;
}

CComplex operator*(CComplex num1,CComplex num2)
{
	CComplex temp;
	temp.real=num1.real*num2.real-num1.virt*num2.virt;
	temp.virt=num1.real*num2.virt+num1.virt*num2.real;
	return temp;
}

void CComplex::operator()()
{
	cout << real;
	if(virt>0) cout << "+" << virt << "i" << endl;
	if(virt<0) cout << virt << "i" << endl;
}

void main()
{
	CComplex com1(1.5,2.4),com2(3.1,4.8),totall1,totall2,totall3;
	totall1=com1+com2;// totall1=operator+(com1,com2);
	totall2=com1-com2;// totall2=operator-(com1,com2);
	totall3=com1*com2;// totall2=operator*(com1,com2);
	
	cout << "输出复数 com1和com2:" << endl;
	cout << "com1=" ;
	com1();
	cout << "com2=";
	com2();

	cout << "---------附属运算---------" << endl;
	cout << "com1+com2=";
	totall1();
	cout << endl;

	cout << "com1-com2=";
	totall2();
	cout << endl;

	cout << "com1*com2=";
	totall3();
	cout << endl;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值