类和对象—复数类

来源:互联网 发布:懒人做饭知乎 编辑:程序博客网 时间:2024/05/21 03:28

      

        C++中较为常见的类—复数类,主要实现复数比较大小、加、减、乘、除等基本运算,通过C++类和对象进行实现。C++的类和对象是笔试、面试中容易问到的,因此,对其掌握一定要通透。下面是复数类功能的具体实现。


#define _CRT_SECURE_NO_WARNINGS 1 //复数类#include <iostream>#include <stdlib.h>using namespace std; class complex{public:     complex(double real = 0.0, double image = 0.0)    //构造函数    {     //cout << "complex(double real, double image)" << endl;       //用于查看构造函数调用多少次          _real = real;          _image = image;     }     complex(const complex & c)   //拷贝构造函数,注意引用符,若为值传递则可能会引发无穷递归    {        //cout << "complex(const complex & c)" << endl;          _real = c._real;          _image = c._image;     }     ~complex()    //析构函数    {        //cout << "~complex" << endl;     }      complex & operator=(const complex& c)   //赋值操作符的重载    {        if (this != &c)        {            this->_real = c._real;            this->_image = c._image;        }        return *this;    }     bool operator==(const complex & c)    //判相等    {         return _real == c._real && _image == c._image;     }     bool operator>(const complex & c)    //大于    {         if ((_real > c._real) || (_real == c._real && _image > c._image))         {              return true;         }                else             return false;    }      bool operator<(const complex & c)   //小于    {         if ((_real < c._real) || (_real == c._real && _image < c._image))         {              return true;         }         else              return false;    }      complex operator+(const complex & c)    //复数加法    {         complex tmp;         tmp._real = _real + c._real;         tmp._image = _image + c._image;         return tmp;     }      complex & operator+=(const complex & c)    //+=     {          _real += c._real;          _image += c._image;          return *this;      }       complex operator-(const complex & c)    //复数减法     {          complex tmp;          tmp._real = _real - c._real;          tmp._image = _image - c._image;          return tmp;     }      complex & operator-=(const complex & c)    //-=     {         _real -= c._real;         _image -= c._image;         return *this;     }      complex operator*(const complex & c)   //复数乘法     {         complex tmp;         tmp._real = _real*c._real - _image*c._image;         tmp._image = _real*c._image + _image*c._real;         return tmp;     }      complex & operator*=(const complex & c)   //复数*=     {         *this = *this * c;         return *this;     }      complex operator/(const complex & c)   //复数除法     {         complex tmp;         // x = (ac + bd) / (cc + dd), y = (bc - ad) / (cc + dd);         tmp._real = (_real * c._real + _image * c._image)                     / (c._real*c._real + c._image*c._image);         tmp._image = (_image * c._real - _real * c._image)                     / (c._real*c._real + c._image*c._image);         return tmp;     }      complex operator/=(const complex & c)   //复数/=     {         *this = *this / c;         return *this;     }      void Display()    //打印     {          cout << _real << "-" << _image << endl;      } private:     double _real;     double _image;};   //测试用例void Test(){     complex p1;     p1.Display();     complex p2(1.0, 2.0);     p2.Display();      complex p3(p1);     p3.Display();     complex p4 = p1;     p4.Display();} void Text1(){     complex p1(2.0, 3.0);     complex p2(4.0, 5.0);     p1.Display();     p2.Display();     bool ret;     ret = p1 == p2;     cout<< ret << endl;     ret = p1 > p2;     cout<< ret << endl;     ret = p1 < p2;     cout<< ret << endl;} void Text2(){    //加减乘除      complex p1(1.0, 2.0);      complex p2(2.0, 3.0);      complex tmp;      tmp = p1 + p2;      tmp.Display();      tmp = p1 - p2;      tmp.Display();      tmp = p1 * p2;      tmp.Display();      tmp = p1 / p2;      tmp.Display();}  int main(){     //Test();     //Text1();       Text2();     system("pause");     return 0;}


本文出自 “无心的执着” 博客,转载请与作者联系!

0 0
原创粉丝点击