码迷,mamicode.com
首页 > 编程语言 > 详细

C++复数类面向对象的参考

时间:2019-02-15 19:54:49      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:因此   amp   使用   lin   std   include   using   future   ons   

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

C++复数类面向对象的参考

标签:因此   amp   使用   lin   std   include   using   future   ons   

原文地址:https://www.cnblogs.com/Q1143316492/p/10385461.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!