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;
}

转载于:https://www.cnblogs.com/Q1143316492/p/10385461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值