#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
class Complex
{
public:
Complex(){real = 0; imag = 0;}
Complex(double r,double i){real = r; imag = i;}
void display(){
cout<<"("<<real<<","<<imag<<")"<<endl;
}
Complex operator + (Complex c2);
friend ostream& operator <<(ostream&,Complex&);
private:
double real;
double imag;
};
Complex Complex :: operator + (Complex c2)
{
Complex x;
x.real = real+c2.real;
x.imag = imag+c2.imag;
return x;
}
ostream& operator <<(ostream& os,Complex& c)
{
cout<<"("<<c.real<<","<<c.imag<<")"<<endl;
return os;
}
int main()
{
Complex c1(3,4), c2(5,-10), c3;
c3 = c1+c2;
return 0;
}
C++ 关于重载
最新推荐文章于 2024-01-24 17:26:37 发布