• 第31课 完善的复数类


    1. 复数类应具有的操作

    (1)运算:+、-、*、/

    (2)比较:==、!=

    (3)赋值:=

    (4)求模:modulus

    2. 利用操作符重载

    (1)统一复数与实数运算方式

    • 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);//赋值

    (2)统一复数与实数比较方式

    • bool operator == (const Complex& c);
    • bool operator != (const Complex& c);

    【编程实验】复数类的实现   Complex.cpp

    //Complex.h

    #ifndef _COMPLEX_H_
    
    #define _COMPLEX_H_
    
     
    
    class Complex
    
    {
    
    private:
    
        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;
    
     
    
        return Complex(na, nb);
    
    }
    
     
    
    Complex Complex::operator - (const Complex& c)
    
    {
    
        double na = a - c.a;
    
        double nb = b - c.b;
    
     
    
        return Complex(na, nb);
    
    }
    
     
    
    Complex Complex::operator * (const Complex& c)
    
    {
    
        double na = a * c.a - b * c.b;
    
        double nb = a * c.b - b * c.a;
    
     
    
        return Complex(na, nb);
    
    }
    
     
    
    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;
    
     
    
        return  Complex(na, nb);
    
    }
    
       
    
    bool Complex::operator == (const Complex& c)
    
    {
    
        return (a == c.a) && (b == c.b);
    
    }
    
     
    
    bool Complex::operator != (const Complex& c)
    
    {
    
        //整个复数对象就两个成员,如果这个2个对象的
    
        //内存完全相等时,则两个复数相等
    
        return !(*this == c);
    
    }
    
     
    
       
    
    Complex& Complex::operator = (const Complex& c)
    
    {
    
        if(this != &c)
    
        {
    
            a = c.a;
    
            b = c.b;
    
        }
    
        return *this;
    
     
    
    }

    //main.cpp

    #include <stdio.h>
    
    #include "Complex.h"
    
     
    
    int main()
    {
    
        Complex c1(1, 2);
    
        Complex c2(3, 6);
    Complex c3
    = c2 - c1; Complex c4 = c1 * c3; Complex c5 = c2 / c1; printf(" "); printf("c3.a = %f, c3.b = %f ",c3.getA(), c3.getB());//2, 4 printf("c4.a = %f, c4.b = %f ",c4.getA(), c4.getB());//-6, 0 printf("c5.a = %f, c5.b = %f ",c5.getA(), c5.getB());//3, 0 printf(" "); Complex c6(2, 4); printf("c3 == c6: %d ", c3 == c6); //1 printf("c3 != c4: %d ", c3 != c4); //1 printf(" "); (c3 = c2) = c1;//赋值操作赋返回值是引用,可以连续赋值 printf("c1.a = %f, c1.b = %f ",c1.getA(), c1.getB());//1, 2 printf("c2.a = %f, c2.b = %f ",c2.getA(), c2.getB());//3, 6 printf("c3.a = %f, c3.b = %f ",c3.getA(), c3.getB());//1, 2 return 0; }

    运行结果:

      

    3. 注意事项

    (1)C++规定赋值操作符=只能重载为成员函数(而不能是全局函数)

    (2)操作符重载改变不了原操作符的优先级

    (3)操作符重载不能改变操作数的个数

    (4)操作符重载不应改变操作符的原有语义

    4. 小结

    (1)复数的概念可以通过自定义类实现

    (2)复数中的运算操作可以通过操作符重载实现

    (3)赋值操作符(=)只能通过成员函数实现

    (4)操作符重载本质为函数定义

  • 相关阅读:
    SQL Server 2005 出现“此数据库没有有效所有者”错误的解决方法
    使用swfupload出现SecurityError Error #2156问题
    读取Excel表
    POJ 1953 (DP)
    POJ 1050 (DP)
    POJ 1276 (DP)
    POJ 1579 (DP)
    HDOJ 4223 (DP)
    POJ 1080 (DP)
    POJ 1458 (DP)
  • 原文地址:https://www.cnblogs.com/hoiday/p/10163390.html
Copyright © 2020-2023  润新知