重新认识c++(01),做个简单的复数Complex类(一)

2014-11-24 10:20:28 · 作者: · 浏览: 2

最近看了一下Effect c++ 想要重新理解一下c++的机制以及内存布局,还是想搞这方面的研究,对底层不明白,总是有点很不踏实。

那就写了一个 Complex 类,这个类还是相对比较全的,基本上涵盖了一些运算。请大神飘过,勿 ,谢谢合作。


Complex.h文件

#ifndef __COMPLEX_H__
#define __COMPLEX_H__
#include 
  
   
#include 
   
     using namespace std ; class Complex{ public: Complex(); Complex(Complex&); Complex(double); Complex(double,double); Complex& operator = (Complex&); ~Complex(); double getA(); double getB(); void setA(double); void setB(double); Complex operator + (double); Complex operator - (double); Complex operator * (double); Complex operator / (double); Complex& operator += (double); Complex& operator -= (double); Complex& operator *= (double); Complex& operator /= (double); bool operator == (double); bool operator == (Complex &c); Complex operator + (Complex&); Complex operator - (Complex&); Complex operator * (Complex&); Complex operator / (Complex&); Complex& operator += (Complex&); Complex& operator -= (Complex&); Complex& operator *= (Complex&); Complex& operator /= (Complex&); private: double a; double b; }; ostream& operator << (ostream &,Complex &); Complex operator + (double,Complex &); Complex operator - (double,Complex &); Complex operator * (double,Complex &); Complex operator / (double,Complex &); Complex pow(Complex &,int); #endif // __COMPLEX_H__ 
   
  

Complex.cpp文件

#include "Complex.h"
Complex::Complex():a(0),b(0){}
Complex::Complex(Complex& c):a(c.getA()),b(c.getB()){}
Complex::Complex(double _a):a(_a),b(0){}
Complex::Complex(double _a,double _b):a(_a),b(_b){}
Complex::~Complex(){};
Complex&  Complex::operator = (Complex&c){
    this->a = c.getA() ;
    this->b = c.getB() ;
    return *this;
}
double Complex::getA(){
    return this->a;
}
double Complex::getB(){
    return this->b;
};

void Complex::setA(double _a){
    this->a = _a ;
}
void Complex::setB(double _b){
    this->b = _b ;
}

bool Complex::operator == (double a){
    Complex c(a);
    return *this == c ;
}
bool Complex::operator == (Complex &c){
    if(this->a == c.getA() && this->b == c.getB())return true;
    return false;
}


Complex& Complex::operator += (Complex&c){
    this->a += c.getA() ;
    this->b += c.getB() ;
    return *this;
}
Complex& Complex::operator -= (Complex&c){
    this->a -= c.getA() ;
    this->b -= c.getB() ;
    return *this;
}
Complex& Complex::operator *= (Complex&c){
    this->a = ((this->a)*(c.getA()) - (this->b)*(c.getB()));
    this->b = ((this->a)*(c.getB()) + (this->b)*(c.getA()));
    return *this;
}
Complex& Complex::operator /= (Complex&c){
    if(c == 0)throw "error" ;
    else {
        *this = (*this) / c ;
    }
    return *this;
}

Complex& Complex::operator += (double a){
    Complex r(a) ;
    *this+=r;
    return *this;
}
Complex& Complex::operator -= (double a){
    Complex r(a) ;
    *this-=r;
    return *this;
}
Complex& Complex::operator *= (double a){
    Complex r(a) ;
    *this*=r;
    return *this;
}
Complex& Complex::operator /= (double a){
    Complex r(a) ;
    *this/=r;
    return *this;
}

Complex Complex::operator + (Complex&c){
    Complex r ;
    r.setA(this->a + c.getA()) ;
    r.setB(this->b + c.getB()) ;
    return r ;
}
Complex Complex::operator - (Complex&c){
    Complex r ;
    r.setA(this->a - c.getA()) ;
    r.setB(this->b - c.getB()) ;
    return r ;
}
Complex Complex::operator * (Complex&c){
    Complex r ;
    r.setA(((this->a)*(c.getA()) - (this->b)*(c.getB()))) ;
    r.setB(((this->a)*(c.getB()) + (this->b)*(c.getA()))) ;
    return r ;
}
Complex Complex::operator / (Complex&c){
    Complex r = c;
    if(c == 0 )throw "error" ;
    double t = c.getA()*c.getA() + c.getB()*c.getB() ;
    r.setB(-1*r.getB()) ;
    r = ((*this) * r) * (1.0/t) ;
    return r;
}

Complex Complex::operator + (double a){
    Complex r(a) ;
    return (*this)+r;
}
Complex Complex::operator - (doub