#include<iostream>
#include<math.h>
#include<string>
using namespace std;
/************************************************************************/
/* 要先对Complex和friend函数进行声明,不然会报错*/
/************************************************************************/
class Complex;
Complex operator+( Complex &, Complex &);
Complex operator-( Complex &, Complex &);
Complex operator*( Complex &, Complex &);
Complex operator/( Complex &, Complex &);
class Complex
{
public:
Complex();
Complex(double);
Complex(double, double);
Complex(double, double, string);
void setReal(double);
void setImag(double);
void setComplex(double, double);
void setName(string);
double getReal();
double getImag();
Complex getComplex();
string getName();
void showComplex();
friend Complex operator+( Complex &,Complex &);
friend Complex operator-( Complex &, Complex &);
friend Complex operator*( Complex &, Complex &);
friend Complex operator/( Complex &, Complex &);
friend ostream & operator<<(ostream & out, Complex &);
/************************************************************************/
/* cin >> i; 可以翻译为 cin.operator>>(i);
同理cout << i; cout.operator<<(i);*/
/************************************************************************/
private:
double real;
double imag;
string name;
};
Complex::Complex()
{
real = 0.0;
imag = 0.0;
name = "complex";
}
Complex::Complex(double r)
{
real = r;
imag = 0.0;
name = "complex";
}
Complex::Complex(double curReal, double curImag)
{
real = curReal;
imag = curImag;
name = "complex";
}
Complex::Complex(double curReal, double curImag, string newName)
{
real = curReal;
imag = curImag;
name = newName;
}
double Complex::getReal()
{
return real;
}
double Complex::getImag()
{
return imag;
}
Complex Complex::getComplex()
{
return Complex(real, imag);
}
string Complex::getName()
{
return name;
}
void Complex::setReal(double curReal)
{
real = curReal;
}
void Complex::setImag(double curImag)
{
imag = curImag;
}
void Complex::setComplex(double r, double i)
{
real = r;
imag = i;
}
void Complex::setName(string newName)
{
name = newName;
}
void Complex::showComplex()
{
cout << real << " + " << imag << "i" << endl;
}
inline Complex operator+ (Complex &a, Complex &b)
{
double newReal = a.getReal() + b.getReal();
double newImag = a.getImag() + b.getImag();
Complex newComplex(newReal, newImag);
return newComplex;
}
Complex operator- (Complex &a, Complex &b)
{
double newReal = a.getReal() - b.getReal();
double newImag = a.getImag() - b.getImag();
Complex newComplex(newReal, newImag);
return newComplex;
}
Complex operator* (Complex &a, Complex &b)
{
double newReal = a.getReal()*b.getReal() - a.getImag()*b.getImag();
double newImag = a.getReal()*b.getImag() + a.getImag()*b.getReal();
Complex newComplex(newReal, newImag);
return newComplex;
}
Complex operator/ (Complex &a, Complex &b)
{
double abs_sq = b.getReal()*b.getReal() + b.getImag()*b.getImag();
double newReal = (a.getReal()*b.getReal() + a.getImag()*b.getImag())/abs_sq;
double newImag = (a.getImag()*b.getReal() - a.getReal()*b.getImag())/abs_sq;
Complex newComplex(newReal, newImag);
return newComplex;
}
ostream operator<<(ostream &out, Complex &c)
{
return out << c.getName() << ": " << c.getReal() << " + " << c.getImag() << "i" << endl;
}
void test()
{
Complex c1;
c1.setName("c1");
c1.showComplex();
Complex c2(1.2);
c2.setName("c2");
c2.showComplex();
Complex c3(3.4, 5.6);
c3.setName("c3");
c3.showComplex();
Complex c4(2, 4, "c4");
c4.showComplex();
c1 = c2 + c3;
c1.showComplex();
c1 = c2 - c3;
c1.showComplex();
c1 = c2*c3;
c1.showComplex();
c1 = c2/c3;
c1.showComplex();
}
int main()
{
test();
return 0;
}
重温操作符重载(二)
最新推荐文章于 2024-05-02 22:08:07 发布