【C++】运算符重载(eg:复数运算)

Complex.h

#include<iostream>

using namespace std;

class Complex {

public:

	Complex(double real = 0.0, double image = 0.0) {
		this->real = real;
		this->image = image;
	}

	~Complex() {

	}

	Complex operator-();
	Complex operator=(const Complex &c);

	Complex operator+(const Complex &c);
	Complex operator-(const Complex &c);
	Complex operator*(const Complex &c);
	Complex operator/(const Complex &c);

	Complex operator+=(const Complex &c);
	Complex operator-=(const Complex &c);
	Complex operator*=(const Complex &c);
	Complex operator/=(const Complex &c);

	double get_real() {
		return real;
	}

	double get_image() {
		return image;
	}

	void print() {
		cout << "Real: " << real << "	Image: " << image << endl;
	}
	

private:
	double real;
	double image;

};

 

Complex.cpp

#include "Complex.h"

Complex Complex::operator-() {
	real = -real;
	image = -image;
	return *this;
}

Complex Complex::operator=(const Complex &c) {
	real = c.real;
	image = c.image;
	return *this;
}


Complex Complex::operator+(const Complex &c) {
	Complex complex;
	complex.real = real + c.real;
	complex.image = image + c.image;
	return complex;
}

Complex Complex::operator-(const Complex &c) {
	Complex complex;
	complex.real = real - c.real;
	complex.image = image - c.image;
	return complex;
}

Complex Complex::operator*(const Complex &c) {
	Complex complex;
	complex.real = real * c.real - (image * c.image);
	complex.image = real * c.image + image * c.real;
	return complex;
}

Complex Complex::operator/(const Complex &c) {
	Complex complex;
	if(c.image == 0) {
		complex.real = real/c.real;
		complex.image = image/c.real;
		return complex;
	}

	double division = c.real * c.real + c.image * c.image;
	Complex d = Complex(c.real, -c.image);
	complex = *this * d;
	complex.real /= division;
	complex.image /= division;
	return complex;
}

Complex Complex::operator+=(const Complex &c) {
	*this = *this + c;
	return *this;
}

Complex Complex::operator-=(const Complex &c) {
	*this = *this - c;
	return *this;
}

Complex Complex::operator*=(const Complex &c) {
	*this = *this * c;
	return *this;
}

Complex Complex::operator/=(const Complex &c) {
	*this = *this / c;
	return *this;
}

 

main.cpp

# include <iostream>
# include "Complex.cpp"

using namespace std;

int main() {
	Complex c1 = Complex(5,6);
	Complex c2 = Complex(3,4);

	Complex cp1 = c1;
	cp1.print();

	Complex cp2 = -c2;
	cp2.print();

	Complex sum = c1 + c2;
	cout <<  "加法	实部: " << sum.get_real() << "	虚部:" << sum.get_image() << "i" << endl;

	Complex minus = c1 - c2;
	cout <<  "减法	实部:" << minus.get_real() << "	虚部:" << minus.get_image() << "i" << endl;

	Complex mul = c1 * c2;
	cout <<  "乘法	实部:" << mul.get_real() << "	虚部:" << mul.get_image() << "i" << endl;

	Complex div = c1 / c2;
	cout <<  "除法	实部:" << div.get_real() << "	虚部:" << div.get_image() << "i" << endl;

	Complex sum1 = c1;
	sum1 +=c2;
	cout <<  "+=	实部: " << sum1.get_real() << "	虚部:" << sum1.get_image() << "i" << endl;

	Complex sum2 = c1;
	sum2 -=c2;
	cout <<  "-=	实部: " << sum2.get_real() << "	虚部:" << sum2.get_image() << "i" << endl;

	Complex sum3 = c1;
	sum3 *=c2;
	cout <<  "*=	实部: " << sum3.get_real() << "	虚部:" << sum3.get_image() << "i" << endl;

	Complex sum4 = c1;
	sum4 /=c2;
	cout <<  "/=	实部: " << sum4.get_real() << "	虚部:" << sum4.get_image() << "i" << endl;

	return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值