分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
以下示例中定义了一个class test, 重载了<, +, +=, =, ==, <<, >>等符号:
#include<iostream>#include<vector>using namespace std;class test{public: int v; test():v(0){} test(const int &a):v(a){} test(const test &t1):v(t1.v){} bool operator<(const test &t1) const{ return (v < t1.v); } bool operator<(const int &t1) const{ return (v < t1); } friend inline bool operator<(const int &a, const test & t1){ return (a < t1.v); } test & operator=(const test &t1){ v = t1.v; return *this; } test & operator=(const int &t1){ v = t1; return *this; } test operator+(const int & a){ test t1; t1.v = v + a; return t1; } test operator+(test &t1){ test t2; t2.v = v + t1.v; return t2; } test &operator+=(const test &t1){ v += t1.v; return *this; } test &operator+=(const int &a){ v += a; return *this; } bool operator==(const test &t1)const{ return (v == t1.v); } bool operator==(const int &t1)const{ return (v == t1); } friend inline ostream & operator << (ostream & os, test &t1){ cout << "class t(" << t1.v << ")" << endl; return os; } friend inline istream & operator >> (istream & is, test &t1){ cin >> t1.v; return is; }};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 cout << "t1 > t2"; cout <<endl; system("pause"); return 0;}