Complex.h
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex {
public:
Complex();
Complex(double,double);
Complex(const Complex&);
~Complex();
Complex operator+(const Complex&);
Complex operator-(const Complex&);
Complex operator*(const Complex&);
Complex operator/(const Complex&);
double getReal();
double getImginary();
void setReal(const double);
void setImginary(const double);
Complex& operator++(); //perfix
Complex operator++(int); //suffix
Complex& operator--(); //perfix
Complex operator--(int); //suffix
private:
double real;
double imginary;
};
#endif // COMPLEX_H
Complex.cpp
#include "Complex.h"
///used for calculate the result of power exponent of base
template<typename T>
T power (T arg0, const int arg1) {
double tmp = 1;
if (arg1 == 0) {
return 1;
} else if (arg1 < 0) {
for (int i = arg1; i!=0;i++)
tmp /= arg0;
return tmp;
} else {
for (int i = arg1; i!=0;i--)
tmp *= arg0;
return tmp;
}
}
Complex::Complex() {
real = 0;
imginary = 0;
}
Complex::Complex(double x, double y) {
real = x;
imginary = y;
}
Complex::Complex(const Complex& p) {
real = p.real;
imginary = p.imginary;
}
Complex::~Complex() {
//dtor
}
Complex Complex::operator+ (const Complex &p) {
Complex tmp(real+p.real, imginary+p.imginary);
return tmp;
}
Complex Complex::operator- (const Complex &p) {
Complex tmp(real-p.real, imginary-p.imginary);
return tmp;
}
Complex Complex::operator* (const Complex &p) {
Complex tmp(real*p.real - imginary*p.imginary,real*p.imginary + imginary*p.real);
return tmp;
}
Complex Complex::operator/ (const Complex &p) {
double real_root = power(power (p.real,2) + power(p.imginary,2),-1);
Complex tmp((real*p.real + imginary*p.imginary)*real_root, (-real*p.imginary + imginary*p.real)*real_root);
return tmp;
}
double Complex::getReal() {
return real;
}
double Complex::getImginary() {
return imginary;
}
void Complex::setReal(const double x) {
real = x;
}
void Complex::setImginary(const double y) {
imginary = y;
}
Complex& Complex::operator++() {
real++;
imginary++;
return *this;
}
Complex Complex::operator++(int) {
Complex tmp(real,imginary);
real++;
imginary++;
return tmp;
}
///-- then LOAD VAR(CHANGED VALUE)
Complex& Complex::operator--() {
real--;
imginary--;
return *this;
}
///LOAD VAR then --
Complex Complex::operator--(int) {
Complex tmp(real,imginary);
real--;
imginary--;
return tmp;
}
main.cpp
#include <iostream>
#include "Complex.h"
#include <iomanip>
using std::cout;
using std::fixed;
using std::setprecision;
void show(Complex& tmp);
int main () {
Complex complex1;
show(complex1);
complex1.setImginary(1); ///complex1=0+i
complex1.setReal(1); ///complex1=1+i
show(complex1);
Complex complex2(1,1); ///complex2=1+i
Complex complex3(complex1+complex2); ///complex3=2+2i
show(complex3);
Complex complex4(complex1*complex2); ///complex4=0+2i
show(complex4);
Complex complex5(complex4/complex3); ///complex5=.5(1+i)
show(complex5);
++complex5; ///1.5(1+i)
show(complex5);
complex5++; ///2.5(1+i)
show(complex5);
--complex5; ///1.5(1+i)
show(complex5);
complex5--; ///.5(1+i)
show(complex5);
return 0;
}
void show(Complex& tmp) {
cout << "The complex number is " << fixed << setprecision(3) << tmp.getReal() << "+" << tmp.getImginary() << "i.\n";
}
基本上顯示了operator overloading是怎樣運作的....可以研究一下