class complex

本文详细介绍了C++中一个名为complex的类,包括构造函数、成员函数(如加法运算符重载)、全局函数以及友元函数的实现,强调了数据成员的私有化和函数参数的引用传递等最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class complex from C++_OOP_base1_houjie

complex.h

#ifndef __COMPLEX__ // 防卫式声明 guard; 名称自定义
#define __COMPLEX__

// 0. forward declarations
class complex;

complex& __doapl (complex* ths, const complex& r);

// 1. class declarations
class complex // class head
{
public: // access level
    // constructor (ctor): the name same with the class name; automatically called;
    // ctor without return type
    complex (double r = 0, double i = 0) // default argument
     : re (r), im (i) // initialization list 初始化要早于{}内赋值
    { } // no return; {}内还可做其他事情,此处无需;
    // operator overloading of member function (has this pointer)
    complex& operator += (const complex&);
    // const member function
    double real () const { return re; } // inline func
    double imag () const { return im; }
private: // visible within class
    double re, im; // data

    friend complex& __doapl (complex*, const complex&);
};

// 2. class definition
// 2.1 member function
// 任何一个member func都有一个隐藏的this pointer, 指向调用者
// 返回值是complex&是考虑到连续使用+=
inline complex& complex::operator += (const complex& r) // param this hidden here
{
    return __doapl (this, r);
}

// 2.2 global function
inline double real (const complex& x)
{
    return x.real();
}

inline double imag (const complex& x)
{
    return x.imag();
}

// operator overloading of non member function, 因为复数不仅仅只能够加复数 ,所以设置为global func而非member func
// return by value, becase the return is a local object (typename () is a temp object 临时对象, e.g., complex (...))
inline complex operator + (const complex& x, const complex& y)
{
    return complex (real (x) + real (y),
                    imag (x) + imag (y));
}

inline complex operator + (const complex& x, double y) // c + 5
{
    return complex (real (x) + y, imag (x));
}

inline complex operator + (double x, const complex& y) // 5 + c
{
    return complex (x + real (y), imag (y));
}

inline complex operator + (const complex& x) // 正号
{
    return x;
}

inline complex operator - (const complex& x)
{
    return complex (-real (x), -imag (x));
}

inline bool operator == (const complex& x, const complex& y)
{
    return real (x) == real (y) && imag (x) == imag (y);
}

inline bool operator == (const complex& x, double y)
{
    return real (x) == y && imag (x) == 0;
}

inline bool operator == (double x, const complex& y)
{
    return real (y) == 0 && imag (y) == x;
}

inline bool operator != (const complex& x, const complex& y)
{
    return real (x) != real (y) || imag (x) != imag (y);
}

// ...

inline complex conj (const complex& x) // 共轭
{
    return complex (real (x), -imag (x));
}

// << 会将右边的东西作用到左边身上,比如作用到cout(标准输出流), 但cout又不 认识complex
// << 不能写成member func,只能写作global func,其他操作符都可以
// 若写作member func,那么左操作数必须是类的对象,然而<< 左边一般是cout...
#include <iostream>
// os不能是const,因为在往cout丢东西的时候,其实都在改变它的状态,所以得允 许修改
// cout的返回值仍然是ostream,所以我们设计的时候返回值是ostream,然后os又不是local的,so by reference.
std::ostream& operator << (std::ostream& os, const complex& x) // param os can be 'cout', which is a obj, and the typename is ostream
{
    return os << "(" << real (x) << ","
              << imag (x) << ")";
}

// __doapl返回指针指向的object,是传递者
// 而complex&是接收者,可以是object,也可以是reference
// 传递者是无需知道接收者以什么形式接受(value or reference)
// __doapl是friend,所以可以直接访问private中的data
inline complex& __doapl (complex* ths, const complex& r)
{
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}

#endif

complex-text.cpp

#include <iostream>
#include "complex.h"

using namespace std;

int main()
{
    complex c1(2, 1);
    complex c2;
    cout << c1 << endl;
    cout << c2 << endl;

    c2 = c1 + 5;
    c2 = 7 + c1;
    c2 = c1 + c2;
    c2 += c1;
    c2 += 3;
    c2 = -c1;

    cout << (c1 == c2) << endl;
    cout << (c1 != c2) << endl;
    cout << conj(c1) << endl;
    return 0;
}

keypoint

  1. 数据放在private中,绝大部分函数放在public中;
  2. 函数传参尽量pass by reference,考虑是否要加const;
  3. 函数返回值尽量return by reference;
  4. constructor使用initialization list;
  5. member function应该加const时需要加上;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值