运算符重载复习

分为:双目运算符重载:> < ==

        单目运算符重载:++  --  ()

        流运算符的重载

 

1、双目运算符重载

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class A
 6 {
 7 public:
 8     A(string s):str(s) {}
 9     friend bool operator < (const A &a1,const A &a2)
10     {
11         if(a1.str < a2.str)
12             return true;
13         else
14             return false;
15     }
16 public:
17     string  str;
18 };
19 
20 int main()
21 {
22     A s1("123");
23     A s2("234");
24     cout << (s1 <s2) << endl;
25     return 0;
26 }

 

2、单目运算符重载

 前置与后置的区别:如果形参为空就是前置,如果在形参中增加一个int型形参,那么就是后置。【调式可以看出他们调用函数的不同...】

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class A
 6 {
 7 public:
 8     A(int s):value(s) {}
 9     A operator++()
10     {
11         ++value;
12         return *this;
13     }
14     A operator++(int a)
15     {
16         value++;
17         return *this;
18     }
19 public:
20     int  value;
21 };
22 
23 int main()
24 {
25     A a(1);
26     cout << (a++).value << endl;  //2
27     cout << (++a).value <<endl;   //3
28     return 0;
29 }

 

小括号运算符

 1 //实例是转载的.
 2 #include <iostream>
 3 using namespace std;
 4 
 5 class Time
 6 {
 7 public:
 8     int h;
 9     int m;
10     int s;
11 
12     Time( int h = 0, int m = 0, int s = 0 )
13     {
14         operator()(h,m,s);
15     }
16 
17     //小括号重载 版本0 注意和下面用户自定义转换的区别
18     int operator()()
19     {
20         return h*3600 + m*60 + s;
21     }
22 
23     //用户自定义转换
24     operator int()
25     {
26         return h*3600 + m*60 + s;
27     }
28 
29     //小括号重载 版本1
30     void operator()(int h)
31     {
32         operator()(h,0,0);
33     }
34 
35     //小括号重载 版本2
36     void operator()(int h, int m)
37     {
38         operator()(h,m,0);
39     }
40 
41     //小括号重载 版本3
42     void operator()(int h, int m, int s)
43     {
44         this->h = h;
45         this->m = m;
46         this->s = s;
47     }
48 
49     friend ostream & operator << ( ostream & os, const Time & t )
50     {
51         os << t.h << "::";
52         if ( t.m < 10 )
53         {
54             os << '0';
55         }
56         os << t.m << "::";
57         if ( t.s < 10 )
58         {
59             os << '0';
60         }
61         os << t.s;
62         return os;
63     }
64 };
65 
66 int main()
67 {
68     /*
69     ** 注意:t(1),t(1,1),t(1,1,1)的用法
70     ** 像不像“函数调用”
71     ** 这样的用法称之为仿函数
72     ** 仿函数在STL里用得特别多
73     */
74     Time t;
75     cout << t << endl;
76     t(1);                    //小括号重载 版本1
77     cout << t << endl;
78     t(1,1);                  //小括号重载 版本2
79     cout << t << endl;
80     t(1,1,1);                //小括号重载 版本3
81     cout << t << endl;
82     cout << t() << endl;     //小括号重载 版本0
83     cout << int(t) << endl;  //用户自定义转换[注意]
84     return 0;
85 }

 

3、流运算符重载

 1 #include <iostream>
 5 using namespace std;
 6 
 7 class A
 8 {
 9 public:
10     A() {}
11     A(int a,int b):key1(a),key2(b) {}
12     /*
13     ** friend istream& operator >> (istream&  ,自定义类型) 这是流运算符重载的固定格式
14     */
15     friend istream& operator >> (istream &is,A &n)
16     {
17         is >> n.key1;
18         is >> n.key2;
19         return is;
20     }
21     /*
22     ** friend ostream& operator >> (ostream&  ,自定义类型) 这是流运算符重载的固定格式
23     */
24     friend ostream& operator << (ostream &out,A &n)
25     {
26         out << n.key1<<endl;
27         out << n.key2;
28         return out;
29     }
30 protected:
31     int key1;
32     int key2;
33 };
34 
35 int main()
36 {
37     A a(1,2);
38     cout << a << endl;
39     A b;
40     cin >> b;
41     cout << b << endl;
42     return 0;
43 }

 

posted on 2012-04-14 12:56  笔记吧... 可能只有自己看得懂  阅读(226)  评论(0编辑  收藏  举报