【C++】 重载操作符类编程

本文介绍了一个通用的复数类模板,并详细展示了如何实现各种运算符的重载,包括加、减、乘、除及自增自减等操作。通过具体的代码实例,演示了复数对象的创建、运算以及输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
using std::ostream; 
using std::cout; 
using std::endl; 
#include<cstdlib>
#include<cmath>

template< class T>
class complex
{ 
	template< class T> 
	friend ostream &operator<<(ostream &, const complex &);

public:
	complex(T r,T i);
	complex operator+(const complex &)const;
	complex operator-(const complex &)const;
	complex operator*(const complex &)const;
	complex operator/(const complex &)const;
	const complex &operator=(const complex &);
	const complex &operator+=(const complex &);
	const complex &operator-=(const complex &);
	const complex &operator*=(const complex &);
	const complex &operator/=(const complex &);
	complex &operator++();
	complex const operator++(int);
	complex &operator--();
	complex const operator--(int);
	const bool operator == (const complex &)const;
	const bool operator != (const complex &)const;

private:
	T real;
	T imaginary;

};

template< class T>
complex<T>::complex(T r, T i):real(r), imaginary(i)//构造函数
{

}

template< class T>
complex<T> complex<T>::operator+(const complex<T> &operand2)const //重载操作符+
{
	return complex(real+operand2.real, imaginary+operand2.imaginary);
}

template< class T>
complex<T> complex<T>::operator-(const complex &operand2)const  //重载操作符-
{
	return complex(real-operand2.real, imaginary-operand2.imaginary);
}

template< class T>
complex<T> complex<T>::operator*(const complex &operand2)const  //重载操作符*
{
	return complex(real*operand2.real-imaginary*operand2.imaginary, real*operand2.imaginary+imaginary*operand2.real);
}

template< class T>
complex<T> complex<T>::operator/(const complex &operand2)const  //重载操作符/
{
	T t;
	t=operand2.real*operand2.real+operand2.imaginary*operand2.imaginary;
	return complex((real*operand2.real+imaginary*operand2.imaginary)/t,
		(imaginary*operand2.real-real*operand2.imaginary)/t);
}

template< class T>
const complex<T>& complex<T>::operator=(const complex &operand2)//重载操作符=
{
	real=operand2.real;
	imaginary=operand2.imaginary;
	return *this;
}

template< class T>
const complex<T>& complex<T>::operator+=(const complex &operand2)//重载操作符+=
{
	real+=operand2.real;
	imaginary+=operand2.imaginary;
	return *this;
}

template< class T>
const complex<T>& complex<T>::operator-=(const complex &operand2)//重载操作符-=
{
	real-=operand2.real;
	imaginary-=operand2.imaginary;
	return *this;
}

template< class T>
const complex<T>& complex<T>::operator*=(const complex &operand2)//重载操作符*=
{
	real=real*operand2.real-imaginary*operand2.imaginary;
	imaginary=real*operand2.imaginary+imaginary*operand2.real;
	return *this;
}

template< class T>
const complex<T>& complex<T>::operator/=(const complex &operand2)//重载操作符/=
{
	T t;
	t=operand2.real*operand2.real+operand2.imaginary*operand2.imaginary;
	real=(real*operand2.real+imaginary*operand2.imaginary)/t;
	imaginary=(imaginary*operand2.real-real*operand2.imaginary)/t;
	return *this;
}

template< class T>
complex<T>& complex<T>::operator++()//重载前缀自增
{
	real++; //先对对象各数据成员增1
	imaginary++;
	return *this; //再返回该对象
}

template< class T>
complex<T> const complex<T>::operator++(int i)//重载后缀自增
{
	complex copy(real, imaginary); //先创建一个临时对象保存原对象的值
	++(*this); //再对对象各数据成员增1
	return copy; //最后返回该对象
}

template< class T>
complex<T>& complex<T>::operator--()//重载前缀自减
{
	real--; //先对对象各数据成员增1
	imaginary--;
	return *this; //再返回该对象
}

template< class T>
complex<T> const complex<T>::operator--(int i)//重载后缀自减
{
	complex copy(real, imaginary); //先创建一个临时对象保存原对象的值
	--(*this); //再对对象各数据成员增1
	return copy; //最后返回该对象
}

template< class T>
const bool complex<T>::operator == (const complex &operand2)const //重载操作符 ==
{
	if(real == operand2.real && imaginary == operand2.imaginary)//当且仅当对象的实部与虚部都对应相等时,对象相等
		return true;
	else
		return false;
}

template< class T>
const bool complex<T>::operator != (const complex &operand2)const //重载操作符 !=
{
	return !(*this == operand2); //操作符 != 与 == 取值相反
}

template< class T>
ostream &operator<<(ostream &output, const complex<T> &operand) //重载操作符<< ,以a+bi形式输出
{
	if(operand.real)  //实部为0时不打印实部 
	{
		cout << operand.real;
		if(operand.imaginary<0)  //虚部为负值时
			cout << operand.imaginary << "i";
		else                         //虚部为正值时
			cout << "+" << operand.imaginary << "i";
	}
	else
		cout << operand.imaginary << "i"; 

	return output;
}

int main()
{
	complex<int> c1(9, 7), c2(7, 9);

	cout << " c1 = " << c1 << endl;
	cout << " c2 = " << c2 << " \n " << endl;


	cout << " c1 + c2 = ";
	complex<int> c3 = c1 + c2;
	cout << c3 << endl;
	cout << " c1 - c2 = ";
	c3 = c1 - c2;
	cout << c3 << " \n " << endl;

	cout << " c3 = ";
	cout << c3 << endl;
	cout << " \n ";

	cout << "c4 = ++c3: " << endl;
	complex<int> c4= ++c3;
	if(c4 == c3)
		cout << " c4等于c3 " << endl;
	else
		cout << " c4不等于c3" << endl;
	cout << " c4 = ";
	cout << c4 << endl;
	cout << " c3 = ";
	cout << c3 << " \n " << endl;


	cout << " c4 = c3++ : " << endl;
	c4 = c3++;
	cout << c3 << " \n " << endl;
	c4 = c3++;
	if(c4 != c3)
		cout << " c4不等于c3 " << endl;
	else
		cout << " c4等于c3 " << endl;
	cout << " c4= ";
	cout << c4 << endl;
	cout << " c3= ";
	cout << c3 << " \n " << endl;

	cout << " c4 = --c3 :" << endl;
	c4 = --c3;
	cout << " c4= ";
	cout << c4 << endl;
	cout << " c3= ";
	cout << c3 << " \n " << endl;

	cout << " c4 = c3-- :" << endl;
	c4 = c3--;
	cout << " c4 = ";
	cout << c4 << endl;
	cout << " c3 = ";
	cout << c3 << " \n " << endl;

	cout << " c1 * c2 = ";
	c3 = c1 * c2;
	cout << c3 << endl;

	cout << " c1 / c2 = ";
	c3 = c1 / c2;
	cout << c3 << " \n " << endl;

	system("pause");
	return 0;
}


 

 c1 = 9+7i
 c2 = 7+9i

 c1 + c2 = 16+16i
 c1 - c2 = 2-2i

 c3 = 2-2i

 c4 = ++c3:
 c4等于c3
 c4 = 3-1i
 c3 = 3-1i

 c4 = c3++ :
4+0i

 c4不等于c3
 c4= 4+0i
 c3= 5+1i

 c4 = --c3 :
 c4= 4+0i
 c3= 4+0i

 c4 = c3-- :
 c4 = 4+0i
 c3 = 3-1i

 c1 * c2 = 130i
 c1 / c2 = 0i

请按任意键继续. . .


仅供参考。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值