极客班GeekBand - C++面向对象高级开发 - 2

本次课程由讲师侯捷带领深入C++面向对象的高级开发,重点复习了Complex类的实现,探讨了拷贝构造、拷贝赋值和析构函数。讲解了栈和堆的内存管理,包括stack objects、heap objects的生命期,并强调了new与delete操作内存的细节,防止内存泄露。此外,还提及了String类的实现过程。
摘要由CSDN通过智能技术生成

极客班GeekBand - C++面向对象高级开发 - 2

讲师 - 侯捷

复习Complex类的实现过程

Complex.h

#ifndef __COMPLEX__
#define __COMPLEX__
#include <iostream>
using namespace std;
class complex {
private:
    double re, im;
public:
    complex(double r = 0, double i = 0) :re(r), im(i) {

    }
    complex& operator += (const complex&);
    double real() const {
        return re;
    }
    double imag() const {
        return im;
    }

    friend complex& __doapl(complex*, const complex&);
};
inline complex& complex::operator+= (const complex& r) {
    return __doapl(this, r);
}
inline complex& __doapl(complex* ths, const complex& r) {
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}
inline complex operator +(const complex& x, const complex& y) {
    return complex(x.real() + y.real(), x.imag() + y.imag());
}
inline complex operator +(const 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值