C 過載operator的示例

2021-09-30 07:38:41 字數 1115 閱讀 4243

#include

#include

using namespace std; class test test(const int &a):v(a){} test(const test &t1):v(t1.v){} /*以下過載小於號 < */ //比較兩個物件的大小 bool operator<(const test &t1) const //比較物件和int的大小 bool operator<(const int &t1) const //友元函式,比較int和物件的大小 friend inline bool operator<(const int &a, const test & t1) /*以下過載賦值號 = */ //物件間賦值 test & operator=(const test &t1) //int賦值給物件 test & operator=(const int &t1) /*以下過載加號 + */ //物件加上 int test operator+(const int & a) //物件加物件 test operator+(test &t1) /*以下過載加等號 += */ //物件加上物件 test &operator+=(const test &t1) //物件加上int test &operator+=(const int &a) /*以下過載雙等號 == */ //物件==物件 bool operator==(const test &t1)const //物件==int bool operator==(const int &t1)const /*以下過載 輸入》 輸出<< */ /*友元函式,輸出物件*/ friend inline ostream & operator << (ostream & os, test &t1) /*友元函式,輸入物件*/ friend inline istream & operator >> (istream & is, test &t1) }; int main(){ test t0, t1(3); test t2(t1); cout << t0 << t1 << t2; cin >> t1; t2 = t1; t2 += t1; t1 += 10; cout << t2; if(t1 < t2) cout << "t1 < t2"; else if(t1 == t2) cout << "t1 = t2"; else /* t1 > t2*/ cout << "t1 > t2"; cout <

C 過載operator的示例

include include using namespace std class test test const int a v a test const test t1 v t1.v 以下過載小於號 比較兩個物件的大小 bool operator const test t1 const 比較物件...

C 過載operator的示例

以下示例中定義了乙個class test,過載了 等符號 include includeusing namespace std class test test const int a v a test const test t1 v t1.v 以下過載小於號 比較兩個物件的大小 bool opera...

C 過載operator的示例

include include using namespace std class test test const int a v a test const test t1 v t1.v 以下過載小於號 比較兩個物件的大小 bool operator const test t1 const 比較物件...