1. 程式人生 > >定義一個複數類,用友元函式實現對雙目運算子“ + ”的運算子過載, 使其適用於複數運算

定義一個複數類,用友元函式實現對雙目運算子“ + ”的運算子過載, 使其適用於複數運算

////定義一個複數類,用友元函式實現對雙目運算子“ + ”的運算子過載,
////使其適用於複數運算

//

#include<iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex()

real = 0;
imag = 0;
}
Complex(int r, int i) :real(r), imag(r) {};
friend Complex operator+(Complex &c1, Complex &c2);
void display();


};

void Complex::display()
{
cout << "real=" << real << endl;
cout << "imag=" << imag << endl;
}
Complex operator+(Complex &c1, Complex &c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main()
{
Complex c1(3, 4);
Complex c2(5, 8);
Complex c3;
c3 = c1 + c2;
c3.display();
return 0;


}