首页 > 其他 > 详细

cpp 面向对象初步探索

时间:2019-09-15 18:18:19      阅读:87      评论:0      收藏:0      [点我收藏+]

需求

尝试定义一个complex(复数类)

简略实现

headers/complex.h

#ifndef __COMPLEX__
#define __COMPLEX__

class complex
{
public:
    complex(double re=0, double im=0):real(re), imag(im)
    { }

    complex& operator += (const complex &other) {
        this->real += other.real;
        this->imag += other.imag;
        return *this;
    }

    double get_real();
    double get_imag();

private:
    double real;
    double imag;
};

double complex::get_real() {
    return this->real;
}

double complex::get_imag() {
    return this->imag;
}
#endif // __COMPLEX__

main.cpp

#include <iostream>
#include "headers/complex.h"
using namespace std;

int main()
{
    complex c1(2, 3);
    complex c2(3, 4);
    c1 += c2;
    cout << c1.get_imag();
    cout << c1.get_real();
}

cpp 面向对象初步探索

原文:https://www.cnblogs.com/Draymonder/p/11523520.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!