c++ 多态性 运算符作为重载函数 复数与实数的相加减

#include <iostream>
using namespace std;
class Complex { //复数类定义
public: //外部接口
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { } //构造函数
Complex operator + (const Complex &c2) const; //运算符+重载成员函数 复数+复数
Complex operator - (const Complex &c2) const; //运算符-重载成员函数  复数-复数
Complex operator + (double &b);     //复数+实数
Complex operator - (double &b);     //复数-实数
friend Complex operator - (double &b,Complex &c2) ;
friend Complex operator + (double &b,Complex &c2) ;
void display() const; //输出复数
//void get_Comp():
private: //私有数据成员
double real; //复数实部
double imag; //复数虚部


};
Complex operator + (double &b,Complex &c2)   //复数+实数
{
    return Complex( b+c2.real,c2.imag);
}
 Complex operator - (double &b,Complex &c2)    //复数-实数
{
    return Complex( b-c2.real,c2.imag);
}
Complex Complex::operator + (const Complex &c2) const { //重载运算符函数实现
return Complex (real + c2.real, imag + c2.imag); //创建一个临时无名对象作为返回值
}


Complex Complex::operator - (const Complex &c2) const { //重载运算符函数实现
return Complex(real - c2.real, imag - c2.imag); //创建一个临时无名对象作为返回值
}
Complex Complex::operator+(double&b)
{
    return Complex(real+b,imag);
}
Complex Complex::operator-(double&b)
{
    return Complex(b-real,imag);
}
void Complex::display() const {
cout << "(" << real << ", " << imag << ")" << endl;
}
int main() { //主函数
Complex c1(5, 4), c2(2, 10),c3,c4,c5;//定义复数类的对象
cout << "c1 = "; c1.display();
cout << "c2 = "; c2.display();
c3 = c1 - c2; //使用重载运算符完成复数减法
cout << "c3 = c1 - c2 = "; c3.display();
c3 = c1 + c2; //使用重载运算符完成复数加法
cout << "c3 = c1 + c2 = "; c3.display();
    double i=3;
c3=c1+i;
cout << "c3 = c1 +i = "; c3.display();
c3=c1-i;
    cout << "c3 = c1 -i = "; c3.display();
    c3=i+c1;
    cout << "c3 = i + c1 = "; c3.display();
    c3=i-c1;
    cout<< "c3 = i - c1 = "; c3.display();
return 0;
}

猜你喜欢

转载自blog.csdn.net/soul778888/article/details/80393910