6 操作符重载实现复数类

(1)、(2)、(3)、(4)代码文件结构:
//Complex.h
#ifndef _COMPLEX_H_#define _COMPLEX_H_class Complex{double a;double b;public:Complex(double a = 0, double b = 0);double getA();double getB();double getModulus();Complex operator + (const Complex& c);Complex operator - (const Complex& c);Complex operator * (const Complex& c);Complex operator / (const Complex& c);bool operator == (const Complex& c);bool operator != (const Complex& c);Complex& operator = (const Complex& c);};#endif
//Complex.cpp
#include "Complex.h"#include "math.h"Complex::Complex(double a, double b){this->a = a;this->b = b;}double Complex::getA(){return a;}double Complex::getB(){return b;}double Complex::getModulus(){return sqrt(a * a + b * b);}Complex Complex::operator + (const Complex& c){double na = a + c.a;double nb = b + c.b;Complex ret(na, nb);return ret;}Complex Complex::operator - (const Complex& c){double na = a - c.a;double nb = b - c.b;Complex ret(na, nb);return ret;}Complex Complex::operator * (const Complex& c){double na = a * c.a - b * c.b;double nb = a * c.b + b * c.a;Complex ret(na, nb);return ret;}Complex Complex::operator / (const Complex& c){double cm = c.a * c.a + c.b * c.b;double na = (a * c.a + b * c.b) / cm;double nb = (b * c.a - a * c.b) / cm;Complex ret(na, nb);return ret;}bool Complex::operator == (const Complex& c){return (a == c.a) && (b == c.b);}bool Complex::operator != (const Complex& c){return !(*this == c);}Complex& Complex::operator = (const Complex& c){if( this != &c ){a = c.a;b = c.b;}return *this;}
//main.cpp
#include <stdio.h>
#include "Complex.h"
int main()
{
Complex c1(1, 2);
Complex c2(3, 6);
Complex c3 = c2 - c1;
Complex c4 = c1 * c3;
Complex c5 = c2 / c1;
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
Complex c6(2, 4);
printf("c3 == c6 : %d\n", c3 == c6);
printf("c3 != c4 : %d\n", c3 != c4);
(c3 = c2) = c1;
printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
return 0;
}
(5)前置,后置操作符
//Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
double a;
double b;
public:
Complex(double a = 0, double b = 0);
double getA();
double getB();
double getModulus();
Complex operator + (const Complex& c);
Complex operator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
bool operator == (const Complex& c);
bool operator != (const Complex& c);
Complex& operator = (const Complex& c);
Complex& operator ++ ();
Complex operator ++ (int);
};
#endif
Complex.cpp
#include "Complex.h"
#include "math.h"
Complex::Complex(double a, double b)
{
this->a = a;
this->b = b;
}
double Complex::getA()
{
return a;
}
double Complex::getB()
{
return b;
}
double Complex::getModulus()
{
return sqrt(a * a + b * b);
}
Complex Complex::operator + (const Complex& c)
{
double na = a + c.a;
double nb = b + c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator - (const Complex& c)
{
double na = a - c.a;
double nb = b - c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator * (const Complex& c)
{
double na = a * c.a - b * c.b;
double nb = a * c.b + b * c.a;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator / (const Complex& c)
{
double cm = c.a * c.a + c.b * c.b;
double na = (a * c.a + b * c.b) / cm;
double nb = (b * c.a - a * c.b) / cm;
Complex ret(na, nb);
return ret;
}
bool Complex::operator == (const Complex& c)
{
return (a == c.a) && (b == c.b);
}
bool Complex::operator != (const Complex& c)
{
return !(*this == c);
}
Complex& Complex::operator = (const Complex& c)
{
if( this != &c )
{
a = c.a;
b = c.b;
}
return *this;
}
Complex& Complex::operator ++ ()
{
a = a + 1;
b = b + 1;
return *this;
}
Complex Complex::operator ++ (int)
{
Complex ret(a, b);
a = a + 1;
b = b + 1;
return ret;
}
1.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i = 0;
i++;
++i;
return 0;
}
2.cpp
#include <iostream>
#include <string>
using namespace std;
class Test
{
int mValue;
public:
Test(int i)
{
mValue = i;
}
int value()
{
return mValue;
}
Test& operator ++ ()
{
++mValue;
return *this;
}
Test operator ++ (int)
{
Test ret(mValue);
mValue++;
return ret;
}
};
int main()
{
Test t(0);
t++;
++t;
return 0;
}
