您好,登錄后才能下訂單哦!
#include<iostream>
using namespace std;
template<class T>
class Complex{
public:
Complex(T a, T b);
void setComplex(T a,T b);
friend Complex<T> operator+(const Complex<T> &c1, const Complex<T> &c2){
//對于模板類的友員函數一定要寫在類內!!!不可以再類外實現
T p_w_picpath = c1.p_w_picpath + c2.p_w_picpath;
T real = c1.real + c2.real;
return Complex<T>(real,p_w_picpath);
//這個必須按照這樣寫,不能加個對象!因為類型不確定!
}
friend Complex<T>operator-(const Complex<T> &c1, const Complex<T> &c2){
T p_w_picpath = c1.p_w_picpath - c2.p_w_picpath;
T real = c1.real - c2.real;
return Complex<T>(real,p_w_picpath);
}
friend Complex<T> operator-(const Complex &c1){
return Complex<T> (-c1.real, -c1.p_w_picpath);
}
void print()const;
private:
T real, p_w_picpath;
};
template <class T>
Complex<T>::Complex(T a, T b){
setComplex(a, b);
}
template<class T>
void Complex<T>::print()const{
cout << "real=" << real << "p_w_picpath=" << p_w_picpath << endl;
}
template <class T>
void Complex<T>::setComplex(T a, T b){
real = a;
p_w_picpath = b;
}
int main(){
Complex<int> a(2, 3);
Complex<int> b(2, 4);
(a + b).print();
(a - b).print();
(-a).print();
system("pause");
return 0;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。