zoukankan      html  css  js  c++  java
  • 22 完善的复数类

    目录

      • 复数类应该具有的操作

        • 运算:+,-,*,/
        • 比较:==,!=
        • 赋值:=
        • 求模:modulus
      • 利用操作符重载

        • 统一复数与实数的运算方式
        • 统一复数与实数的比较方式
        Complex operator + (const Complex& c);
        Complex operator - (const Complex& c);
        Complex operator * (const Complex& c);
        Complex operator / (const Complex& c);
        
        bool operator == (const Complex& c);
        bool operator != (const Complex& c);
        
        Complex& operator = (const Complex& c);
        
      • 示例:复数类的实现

        • Demo

          //Complex.h
          #ifndef _COMPLEX_H_
          #define _COMPLEX_H_
          
          class Complex
          {
              double a;
              double b;
          public:
              Complex(double a = 0, double b = 0);
              double getA();
              double getB();
              double getModulus();
              
              Complex operator + (const Complex& c);
              Complex operator - (const Complex& c);
              Complex operator * (const Complex& c);
              Complex operator / (const Complex& c);
              
              bool operator == (const Complex& c);
              bool operator != (const Complex& c);
              
              Complex& operator = (const Complex& c);
          };
          
          #endif
          
          
          //Complex.cpp
          #include "Complex.h"
          #include "math.h"
          
          Complex::Complex(double a, double b)
          {
              this->a = a;
              this->b = b;
          }
          
          double Complex::getA()
          {
              return a;
          }
          
          double Complex::getB()
          {
              return b;
          }
          
          double Complex::getModulus()
          {
              return sqrt(a * a + b * b);
          }
          
          Complex Complex::operator + (const Complex& c)
          {
              double na = a + c.a;
              double nb = b + c.b;
              Complex ret(na, nb);
              
              return ret;
          }
          
          Complex Complex::operator - (const Complex& c)
          {
              double na = a - c.a;
              double nb = b - c.b;
              Complex ret(na, nb);
              
              return ret;
          }
          
          Complex Complex::operator * (const Complex& c)
          {
              double na = a * c.a - b * c.b;
              double nb = a * c.b + b * c.a;
              Complex ret(na, nb);
              
              return ret;
          }
          
          Complex Complex::operator / (const Complex& c)
          {
              double cm = c.a * c.a + c.b * c.b;
              double na = (a * c.a + b * c.b) / cm;
              double nb = (b * c.a - a * c.b) / cm;
              Complex ret(na, nb);
              
              return ret;
          }
              
          bool Complex::operator == (const Complex& c)
          {
              return (a == c.a) && (b == c.b);
          }
          
          bool Complex::operator != (const Complex& c)
          {
              return !(*this == c);
          }
              
          Complex& Complex::operator = (const Complex& c)
          {
              if( this != &c )
              {
                  a = c.a;
                  b = c.b;
              }
              
              return *this;
          }
          
      • 注意事项

        • C++ 规定赋值操作符(=)只能重载为成员函数
        • 操作符重载不能改变原操作符的优先级
        • 操作符重载不能改变操作数的个数
        • 操作符重载不应改变操作符的原有语义
    • 相关阅读:
      Advanced Sort Algorithms
      Bash Excercises
      分布式Java应用与实践 (一)
      Configure HttpClient correctly
      Automated Front End Test
      linux 判断一个用户是否存在 _fei
      linux 系统扩容 VMware Centos---VMware ESXi
      ESX 基本使用 _fei
      centos jira wiki 开机自启
      svn 添加子目录后检出失败 _fei
    • 原文地址:https://www.cnblogs.com/bky-hbq/p/13724460.html
    Copyright © 2011-2022 走看看