程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> 初識c++,復數類

初識c++,復數類

編輯:關於C++
//#include<iostream>
//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<iostream>
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;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved