初识c++,复数类

2015-11-21 00:55:08 · 作者: · 浏览: 5
//#include
//using namespace std;
//class  Oxen{
//  public:
//    Oxen(double  real = 1.0, double imaginary = 1.0)
//    {
//        _real = real;
//        _imaginary = imaginary;
//    }
//    void  Display();
//    Oxen  Add(Oxen &a, Oxen &b);
//  //private:
//    double  _real;
//    double  _imaginary;
//};
//void Oxen::Display()
//{
//  printf("%d+%di\n", _real, _imaginary);
//}
//Oxen  Add(Oxen &a, Oxen &b)
//{
//  Oxen  ret;
//  ret._real = a._real + b._real;
//  ret._imaginary = a._imaginary + b._imaginary;
//  return  ret;
//}
//int  main()
//{
//  Oxen  value(2);
//  Oxen  val(2, 3);
//  Oxen  ret = Add(value, val);
//  value.Display();
//  ret.Display();
//  system("pause");
//  return 0;
//}
#include
using  namespace std;
class Complex
{
    // 构造函数(无参、带参、带缺省值、半缺省值的构造函数都去练练 )
    // 析构函数
    // 拷贝构造函数
    // 赋值运算符重载
    public:
        /*Complex()
        {
            _real = 1.0;
            _image = 1.0;
            cout << "构造函数" << endl;
        }
        Complex(double real, double  image)
        {
            _real = real;
            _image = image;
        }
        Complex(double  real, double image = 1.0)
        {
            _real = real;
            _image = image;
        }*/
        Complex(double  real = 1.0, double image = 1.0)
        {
            _real = real;
            _image = image;
        }
         
        Complex(Complex& a)
        {
            _image = a._image;
            _real = a._real;
        //  cout << "复制构造" << endl;
        }
         
        ~Complex()
        {
            //cout << "析构函数" << endl;
        }
        Complex& operator ++();    // 前置 ++
        Complex operator ++(int);  // 后置++
        Complex& operator --();   // 前置 -
        Complex operator --(int); // 后置-
        Complex operator +(const Complex& c);
        Complex operator -(const Complex& c);
        Complex& operator -=(const Complex& c);
        Complex& operator +=(const Complex& c);
        Complex operator *(const Complex& c);
        Complex operator /(const Complex& c);
        void  Display();
    private:
        double  _real;
        double  _image;
    };
Complex& Complex:: operator++()
{
    ++this->
_real; ++this->_image; return *this; } Complex Complex::operator++(int)//int是个标记表示后置++,只作为函数重载特征 { Complex tmp; tmp._real = this->_real++; tmp._image = this->_image++; return tmp; } Complex& Complex:: operator--() { --this->_real; --this->_image; return *this; } Complex Complex::operator--(int)//int是个标记表示后置--,只作为函数重载特征 { Complex tmp; tmp._real = this->_real--; tmp._image = this->_image--; return tmp; } Complex Complex::operator+(const Complex &c) { this->_real += c._real; this->_image += c._image; return *this; } Complex Complex::operator-(const Complex &c) { this->_real -= c._real; this->_image -= c._image; return *this; } Complex & Complex::operator+=(const Complex & c) { this->_real += c._real; this->_image += c._image; return *this; } Complex & Complex:: operator-=(const Complex & c) { this->_real -= c._real; this->_image -= c._image; return *this; } Complex Complex:: operator*(const Complex &c) { //(a+bi)(c+di)=(ac-bd)+(bc+ad)i; Complex tmp; tmp._real = (this->_real*c._real) - (this->_image*c._image); tmp._image = (this->_image*c._real) + (this->_real*c._image); return tmp; } Complex Complex:: operator/(const Complex &c) { //(a+bi)/(c+di)=x+yi,x=(ac+bd)/(c*c+d*d),y=(bc-ad)/(c*c+d*d) Complex tmp; tmp._real = ((this->_real*c._real) + (this->_image*c._image)) / (c._real*c._real + c._image*c._image); tmp._image = ((this->_image*c._real) - (this->_real*c._image)) / (c._real*c._real + c._image*c._image); return tmp; } void Complex::Display() { cout << "real:" << _real; cout <<" image"<< _image << endl; } void Test1() { Complex a, b; a.Display(); Complex c=++a;//前置调用 c.Display(); Complex d; d=b++; d.Display(); a=a + b; a.Display(); //a + (b,9);//逗号表达式 } void Test2() { Complex a(1, 2); Complex b(3, 4); /*Complex c = a*b; c.Display();*/ a -= b; a.Display(); } int main() { Test2(); system("pause"); return 0; }

?