C++运算符重载(以复数为例)

#include <iostream>
// using namespace std;
using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;

class Complex {
public:
	Complex();											// default constructor
	Complex( double );									// constructor
	Complex( double, double );							// constructor
	Complex& operator=( const Complex& );				// overload the assignment operator
	Complex& operator+=( const Complex& );				// overload operator +=
	Complex& operator-=( const Complex& );				// overload operator -=
	Complex& operator*=( const Complex& );				// overload operator *=
	Complex& operator/=( const Complex& );				// overload operator /=
	Complex& operator++();								// overload the prefix self-addition
	Complex& operator--();								// overload the prefix self-subtration
	Complex operator++( int );							// overload the suffix self-addition
	Complex operator--( int );							// overload the suffix self-subtration
	void print() const;									// the function to output the complex
	friend Complex operator+( const Complex&, const Complex& );		// overload the addition operator
	friend Complex operator-( const Complex&, const Complex& );		// overload the subtration operator
	friend Complex operator*( const Complex&, const Complex& );		// overload the multiplication operator
	friend Complex operator/( const Complex&, const Complex& );		// overload the division operator
	friend bool operator==( const Complex&, const Complex& );		// overload the equivalent operator
	friend bool operator!=( const Complex&, const Complex& );		// overload the unequal operator
	friend ostream& operator<<( ostream&, const Complex& );
	friend istream& operator>>( istream&, Complex& );
private:
	double real;				// the real part of the complex
	double imag;				// the imaginary part of the complex
};

/**
 * The default constructor to create a complex without parameter.
 * Both the real part and imaginary part are made zero.
 *
 */
Complex::Complex() {

	real = 0;
	imag = 0;

}

/**
 * The constructor to create a complex with one paramater to set the real part,
 * the imaginary part is made zero.
 */
Complex::Complex( double r ) {

	real = r;
	imag = 0;

}

/**
 * The constructor to create a complex with two paramaters.
 */
Complex::Complex( double r, double i ) {

	real = r;
	imag = i;

}

/**
 * The program is to overload the assignment operator with a paramater.
 * 
 * @param t       the complex to assign.
 * @return        the self object
 */ 
Complex& Complex::operator=( const Complex& t ) {

	real = t.real;			// assign the real part of the paramater to the object
	imag = t.imag;			// assign the imaginary part of the paramater to the object

	return *this;			

}

/**
 * This program is to overload the operator +=,with a complex paramater.
 *
 * @param t   the complex to be added to the objext
 * @return    self object
 */
Complex& Complex::operator+=( const Complex& t ) {

	real = real + t.real;	// add the real part of the paramater to the real part of the object
	imag = imag + t.imag;	// add the imaginary part of the paramater to the imaginary part of the object

	return *this;

}

/**
 * This program is to overload the operator -=,with a complex paramater.
 *
 * @param t   the complex to be subtracted to the objext
 * @return    self object
 */
Complex& Complex::operator-=( const Complex& t ) {

	real = real - t.real;	// subtract the real part
	imag = imag - t.imag;	// subtract the imaginary part

	return *this;

}

/**
 * This program is to overload the operator *=,with a complex paramater.
 *
 * @param t   the complex to be multiplied to the objext
 * @return    self object
 */
Complex& Complex::operator*=( const Complex& t ) {
	double temp1;			// the first intermediate variable
	double temp2;			// the second intermediate variable

	temp1 = real;
	temp2 = imag;

	// calculate the result and set it to the object 
	real = temp1 * t.real - temp2 * t.imag;
	imag = temp1 * t.imag + temp2 * t.real;
	
	return *this;

}

/**
 * This program is to overload the operator /=,with a complex paramater.
 *
 * @param t   the complex to be divided to the objext
 * @return    self object
 */
Complex& Complex::operator/=( const Complex& t ) {
	double temp1;			// the first intermediate variable
	double temp2;			// the second intermediate variable

	temp1 = real;
	temp2 = imag;

	// calculate the result and set it to the object
	real = ( temp1 * t.real + temp2 * t.imag ) / ( t.real * t.real + t.imag * t.imag );
	imag = ( temp2 * t.real - temp1 * t.imag ) / ( t.real * t.real + t.imag * t.imag );

	return *this;

}

/**
 * This program is to overload the operator prefix ++,without paramater.
 *
 * @return    self object
 */
Complex& Complex::operator++() {

	real++;					// the real part add one
	imag++;					// the imaginary part add one 

	return *this;

}

/**
 * This program is to overload the operator prefix --,without paramater.
 *
 * @return    self object
 */
Complex& Complex::operator--() {

	real--;					// the real part minus one
	imag--;					// the imaginary part minus one

	return *this;

}

/**
 * This program is to overload the operator suffix ++,without paramater.
 *
 * @return    the complex before add
 */
Complex Complex::operator++( int ) {

	Complex temp( real, imag );		// set the real part and imaginary part to another complex object
	real++;							// the real part add one
	imag++;							// the imaginary add one

	return temp;

}

/**
 * This program is to overload the operator suffix --,without paramater.
 *
 * @return   the object before minus
 */
Complex Complex::operator--( int ) {

	Complex temp( real, imag );		// set the real part and imaginary part to another complex object
	real--;							// the real part minus one
	imag--;							// the imaginary part minus one

	return temp;

}

/**
 * This program is to overload the operator +,with tow complex paramaters.
 *
 * @param t   the first addend
 * @param u   the second addend
 * @return    a complex object
 */	
Complex operator+( const Complex& t, const Complex& u ) {

	return Complex( t.real + u.real, t.imag + u.imag );

}

/**
 * This program is to overload the operator -,with two complex paramaters.
 *
 * @param t   the first subtrahend
 * @param u   the second subtrahend
 * @return    a complex object
 */
Complex operator-( const Complex& t, const Complex& u ) {

	return Complex( t.real - u.real, t.imag - u.imag );

}

/**
 * This program is to overload the operator *,with two complex paramaters.
 *
 * @param t   the first multiplier
 * @param u   the second multiplier
 * @return    a complex object
 */
Complex operator*( const Complex& t, const Complex& u ) {

	return Complex( t.real * u.real - t.imag * u.imag, t.real * u.imag + t.imag * u.real );

}

/**
 * This program is to overload the operator /,with two complex paramaters.
 *
 * @param t   the dividend
 * @param u   the divisor
 * @return    a complex object
 */
Complex operator/( const Complex& t, const Complex& u ) {

	return Complex( ( t.real * u.real + t.imag * u.imag ) / ( u.real * u.real + u.imag * u.imag ), ( t.imag * u.real - t.real * u.imag ) / ( u.real * u.real + u.imag * u.imag ) );

}

/**
 * This program is to overload the operator ==,with two complex paramaters.
 * If both the real part and imaginary part are equal,return true;otherwise,
 * return false.
 *
 * @param t   the first complex to be compared
 * @param u   the second complex to be compared
 * @return    true or false
 */
bool operator==( const Complex& t, const Complex& u ) {

	if ( t.real == u.real && t.imag == u.imag ) {	// both the real part and imaginary part are equal

		return true;

	}	// end if

	else {		// not both the real part and imaginary part are equal

		return false;

	}	// end else

}

/**
 * This program is to overload the operator !=,with two complex paramaters.
 * If not both the real part and imaginary part are equal,return true;otherwise,
 * return false.
 *
 * @param t   the first complex to be compared
 * @param u   the second complex to be compared
 * @return    true or false
 */
bool operator!=( const Complex& t, const Complex& u ) {

	return !( t == u );

}
ostream& operator<<( ostream& os, const Complex& c) {
	os << c.real << "+" << c.imag << "i";
	return os;
}
istream& operator>>( istream& is, Complex& c) {
	is >> c.real >> c.imag;
	return is;
}
/**
 * This program is to output complex object in a format.
 */
void Complex::print() const {

	cout << "(" <<real << "," << imag << ")" << endl;

}

int main()
{
	Complex x( 1, 1 );
	Complex x1( 1, 1 );
	Complex y( 2, 2 );
	Complex z( 4, 4 );

	// invoke operator==
	if( x == x1 ) {

		cout << "The same!\n";

	}

	// invoke operator!=
	if ( x != y ) {

		cout << "Not same!\n";

	}

	// invoke operator=
	cout << "\nBefore assign,x1=";
	x1.print();
	x1 = y;
	cout << "After assign,x1=";
	x1.print();

	// invoke operator+=
	cout << "\nBefore the operator+=\n";
	cout << "x=";
	x.print();
	cout << "y=";
	y.print();
	x += y;
	cout << "After x+=y\n";
	cout << "x=";
	x.print();
	
	// invoke operator-=
	cout << "\nBefore the operator-=\nx=";
	x.print();
	cout << "y=";
	y.print();
	x -= y;
	cout << "After x-=y\nx=";
	x.print();

	// invoke operator*=
	cout << "\nBefore the operator*=\nx=";
	x.print();
	cout << "y=";
	y.print();
	x *= y;
	cout << "After x*=y\nx=";
	x.print();

	// invoke operator/=
	cout << "\nBefore the operator/=\nx=";
	x.print();
	cout << "y="; 
	y.print();
	x /= y;
	cout << "After x/=y\nx=";
	x.print();

	// invoke operator+
	cout << "\nInvoke the operator+\nx=";
	x.print();
	cout << "y=";
	y.print();
	z = x + y;
	cout << "z=x+y=";
	z.print();

	// invoke operator-
	cout << "\nInvoke the operator-\nx=";
	x.print();
	cout << "y=";
	y.print();
	z = x - y;
	cout << "z=x-y=";
	z.print();

	// invoke operator*
	cout << "\nInvoke the operator*\nx=";
	x.print();
	cout << "y=";
	y.print();
	z = x * y;
	cout << "z=x*y=";
	z.print();

	// invoke operator/
	cout << "\nInvoke the operator/\nx="; 
	x.print();
	cout << "y=";
	y.print();
	z = x / y;
	cout << "z=x/y=";
	z.print();

	// invoke operator prefix ++
	cout << "\nInvoke the operator prefix++\nx=";
	x.print();
	z = ++x;
	cout << "After z=++x,\nz="; 
	z.print();
	cout << "x=";
	x.print();

	// invoke operator prefix --
	cout << "\nInvoke the operator prefix--\nx=";
	x.print();
	z = --x;
	cout << "After z=--x,\nz=";
	z.print();
	cout << "x=";
	x.print();

	// invoke operator suffix ++
	cout << "\nInvoke the operator suffix++\nx=";
	x.print();
	z = x++;
	cout << "After z=x++,\nz=";
	z.print();
	cout << "x=";
	x.print();

	// invoke operator suffix --
	cout << "\nInvoke the operator suffix--\nx=";
	x.print();
	z = x--;
	cout << "After z=x--,\nz=";
	z.print();
	cout << "x=";
	x.print();

	cin >> x;
	cout << x << endl;
	return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值