利用运算符重载复数加减

2017-07-24  本文已影响0人  Leo_2dab
#include"iostream"
class Complex{
public:
    Complex(double r=0,double i=0){
        real = r;
        img = i;
    }
    Complex operator+(const Complex &);
    Complex operator-(const Complex &);
    double real;
    double img;
};
Complex Complex::operator+(const Complex& op2){
    return Complex(op2.real + real, op2.img + img);
}
Complex Complex::operator-(const Complex& op2){
    return Complex(real - op2.real, img - op2.img);
}
using namespace std;
int main(){
    Complex y(5.2, 1.3), z(4.4, 7.7);
    Complex x(y);
    z = x + y;
    z = x - y;
    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读