#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r; imag=i;}
Complex operator+(const Complex &c2);
Complex operator-(const Complex &c2);
Complex operator*(const Complex &c2);
Complex operator/(const Complex &c2);
void display();
private:
double real;
double imag;
};
//下面定义成员函数
Complex Complex::operator+(const Complex &c2)
{
return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator-(const Complex &c2)
{
return Complex(real-c2.real,imag-c2.imag);
}
Complex Complex::operator*(const Complex &c2)
{
return Complex(real*c2.real-imag*c2.imag,real*c2.imag+c2.real*imag);
}
Complex Complex::operator/(const Complex &c2)
{
return Complex((real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(-real*c2.imag+
第8周项目1(1)-用成员函数实现复数类中的运算符重载
最新推荐文章于 2020-09-27 15:40:02 发布
本文展示了如何在C++中通过成员函数实现复数类的加、减、乘、除运算符重载。代码包括了复数类的定义、运算符重载的方法以及main函数中的测试用例。
摘要由CSDN通过智能技术生成