zoukankan      html  css  js  c++  java
  • 设计、定义并实现Complex类

    实验结论


    Complex类

    Code:

    #include<iostream>
    #include<cmath>
    #include<cstdlib>
    using namespace std;
    class Complex {
    public:
        Complex(double newr = 0, double newi = 0);
        Complex(Complex &c1);
        void add(Complex c1);
        void show();
        double mod();
    private:
        double real;
        double imaginary;
    };
    //构造函数
    Complex::Complex(double newr/*=0*/, double newi/*=0*/) :real(newr), imaginary(newi) {}
    //复制构造函数
    Complex::Complex(Complex &c1) : real(c1.real), imaginary(c1.imaginary) {}
    //复数相加
    void Complex::add(Complex c1) {
        real += c1.real;
        imaginary += c1.imaginary;
    }
    //输出
    void Complex::show() {
        if (imaginary != 0)
            cout << real << "+" << imaginary << "i" << endl;
        else
            cout << real << endl;
    }
    //取模
    double Complex::mod() {
        return sqrt(real*real + imaginary * imaginary);
    }
    int main() {
        Complex c1(3,5);
        Complex c2 = 4.5;
        Complex c3(c1);
        Complex c4;
        c1.add(c2);
        cout << "c1=";
        c1.show();
        cout << "c2=";
        c2.show();
        cout << "c3=";
        c3.show();
        cout << "c4=";
        c4.show();
        cout << "c1.mod="<<c1.mod()<<endl;
        system("pause");
        return 0;
    }

    Screensort:

    c1为与c2相加后的值

    c3为c1的初值

    c4为默认值

    实验总结与体会


    1.设计类时需要将所有可能出现的情况考虑在内,以此设计参数。

    点评

    https://www.cnblogs.com/laboratory-X/p/10639996.html

     https://www.cnblogs.com/elise00/p/10637773.html

    https://www.cnblogs.com/KOKODA/p/10631865.html

  • 相关阅读:
    文档撰写思路与排版(hadoop)
    ULUA的简洁用法(二)
    开源cocos2d-x编辑器 qco-editor
    u3d tolua + ZeroBraneStudio远程调试
    ULUA的简洁用法
    OpenGL顶点数据传输速度优化
    在do while语句中使用continue的误解
    cocos2d-x 3D shader的纹理坐标是上下颠倒的
    使用ndk-gdb调试android native程序
    OpenSSL中AES加密的用法
  • 原文地址:https://www.cnblogs.com/wyf-blogs/p/10626511.html
Copyright © 2011-2022 走看看