zoukankan      html  css  js  c++  java
  • C++复数类面向对象的参考

    #include <bits/stdc++.h>
    #include <future>
    #include <thread>
    
    using namespace std;
    
    class Complex
    {
    public:
        Complex (double r = 0, double i = 0)
            : re (r), im (i) {}                 ///冒号后面是初始化的过程,注意分清初始化和赋值的区别
        Complex& operator += (const Complex&);
        double real() const { return re; }      ///如果函数内不修改值尽量使用函数const,为后来做打算
        double imag() const { return im; }
    private:
        double re, im;
    
        friend Complex& __doapl(Complex *, const Complex&);
    };
    
    inline Complex&
    __doapl(Complex *ths, const Complex &r)
    {
        ths->re += r.re;
        ths->im += r.im;
        return *ths;
    }
    
    inline Complex&
    Complex::operator += (const Complex& r)
    {
        return __doapl(this, r);
    }
    
    inline Complex
    operator + (const Complex &x, const Complex &y)
    {
        return Complex(x.real() + y.real(), x.imag() + y.imag());
    }
    
    /**
    记录这篇博客主要是看了侯捷老师设计每一个函数都经历了下面的过程,觉得这种思维是比较好的,因此而写博客记录
    
    当我们设计一个函数的时候做如下思考
    参数列表:
        1,我们尽量使用引用的方式传,如果不希望被修改就使用const
        2, 返回类型,思考return by references 还是 return by value.
           例如我们返回os,os是原来就有的东西,所以我们返回引用
           如果是一个在函数里面生成的空间,那么最后返回最后是值
           例如下面重载,我们需要考虑用户可能的cout << a << b;的多重输出。
           因此需要返回ostream,每个地方多为用户想一点
    */
    inline ostream&
    operator << (std::ostream &os, const Complex &x)
    {
        os << "(" << x.real() << "," << x.imag() << ")";
        return os;
    }
    
    int main () {
    
        return 0;
    }
    
    
  • 相关阅读:
    (转)PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
    Java 异步转同步 ListenableFuture in Guava
    makepy
    Eclipse安装Freemarker插件
    Windows下Go语言LiteIDE下载及安装
    Windows 平台下 Go 语言的安装和环境变量设置
    phpcms v9表单向导添加验证码
    mac下谷歌chrome浏览器的快捷键
    vscode的go插件安装
    golang查看文档
  • 原文地址:https://www.cnblogs.com/Q1143316492/p/10385461.html
Copyright © 2011-2022 走看看