复数加法重载函数不作为成员函数

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 class Complex
 6 {
 7     public:
 8         Complex(){
 9             real=0;
10             imag=0;
11         }
12         Complex(double r,double i){
13             real=r;
14             imag=i;
15         }
16         friend Complex operator+(Complex &c1,Complex &c2);
17         void display();
18     private:
19         double real;
20         double imag;
21 };
22 
23 Complex operator+(Complex &c1,Complex &c2)
24 {
25     return Complex(c1.real+c2.real,c1.imag+c2.imag);
26 }
27 
28 void Complex::display()
29 {
30     cout<<"("<<real<<","<<imag<<"i)"<<endl;
31 }
32 int main(int argc, char** argv) {
33     Complex c1(3,4),c2(5,-10),c3;
34     c3=c1+c2;
35     cout<<"c1=";c1.display();
36     cout<<"c2=";c2.display();
37     cout<<"c1+c2=";c3.display();
38     return 0;
39 }
原文地址:https://www.cnblogs.com/borter/p/9405368.html