标签:cout += 实现 定义 复数类 The complex private headers
尝试定义一个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();
}
标签:cout += 实现 定义 复数类 The complex private headers
原文地址:https://www.cnblogs.com/Draymonder/p/11523520.html