c++之运算符重载二

重载++和--

设  A  Aobject ;

运算符 ++和 - - 有两种方式:

 

前置方式:   ++Aobject  --Aobject

一元  成员函数  重载  A :: A operator++ () ;  

  解释为:  Aobject . operator ++( ) ;

          友元函数  重载  friend A operator++ (A &) ;

  解释为:   operator ++( Aobject ) ;  

 

后置方式:  Aobject ++  Aobject

二元  成员函数  重载  A :: A  operator++ (int) ;

  解释为:   Aobject . operator ++( 0 ) ;

          友元函数  重载:  friend A operator++ (A &, int) ;  

  解释为:   operator++(Aobject, 0) 

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 //使用成员函数重载++运算符
 5 class Increase
 6 {
 7 public:
 8     Increase()
 9     {
10         value =0;
11     }
12     void display();
13     Increase operator ++();
14     Increase operator ++(int);
15 private:
16     int value;
17 };
18 //前置
19 Increase Increase::operator++()
20 {
21     value++;
22     return *this;
23 }
24 //后置
25 Increase Increase::operator++(int)
26 {
27     Increase temp;
28     temp.value= value ++;
29     return temp;
30 }
31 void Increase::display()
32 {
33     cout << value <<endl;
34 }
35 void main()
36 {
37     Increase a,b,c;
38     int i;
39     for(i=0;i<10;i++)
40     {
41         //用c调用前置++运算符
42         a = c++;
43     }
44     cout << "c=" ;
45     c.display();
46     cout << "a=";
47     a.display();
48 
49     for(i=0;i<10;i++)
50     {
51         //用c调用后置++运算符
52         b = ++c;
53     }
54     cout << "c=" ;
55     c.display();
56     cout << "b=";
57     b.display();
58 }

使用友元函数通过重载++运算符,实现和上面同样的效果

 1 #include <iostream>
 2 using namespace std;
 3 
 4 //使用友元函数重载++运算符
 5 class Increase
 6 {
 7 public:
 8     Increase()
 9     {
10         value =0;
11     }
12     void display()
13     {
14         cout << value << endl;
15     }
16     friend Increase operator++(Increase &i);
17     friend Increase operator++(Increase &i,int);
18 private:
19     int value;
20 };
21 
22 Increase operator ++(Increase &i)
23 {
24     i.value ++;
25     return i;
26 }
27 Increase operator ++(Increase &i,int)
28 {
29     Increase temp;
30     temp.value = i.value ++;
31     return temp;
32 }
33 void main()
34 {
35     Increase a,b,c;
36     for(int i=0;i<10;i++)
37     {
38         a = c++;
39     }
40     cout << "a=";
41     a.display();
42     cout << "c=";
43     c.display();
44 
45     for(int i=0;i<10;i++)
46     {
47         b = ++c;
48     }
49     cout << "b=";
50     b.display();
51     cout << "c=";
52     c.display();
53 }
“=”赋值运算符重载用于对象数据的复制
 operator= 必须重载为成员函数
重载函数原型为:

  类名  &  类名  :: operator= ( 类名 ) ;

 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 class Name
 6 {
 7 public:
 8     Name(char* name);
 9     Name(Name& name);
10     Name operator =(Name& name);
11     ~Name();
12 private:
13     int size;
14     char* name;
15 };
16 
17 //重载=运算符
18 Name Name::operator=(Name& name)
19 {
20     cout << "重载=运算符\t"<< name.name << endl;
21     //删除当前name的值
22     delete this->name;
23     //为当前的name初始化指定的大小
24     this->name = new char[strlen(name.name)+1];
25     //如果参数name的内容不为空的话,将他的值赋值给当前name
26     if(this->name!=0)
27     {
28         strcpy(this->name,name.name);
29     } 
30     //计算字符的大小
31     size = strlen(name.name);
32     //调用复制构造函数返回当前的this指针
33     return *this;
34 }
35 
36 //构造函数
37 Name::Name(char* name)
38 {
39     cout << "constructing\t" << name << endl;
40     this->name = new char[strlen(name)+1];
41     if(this->name!=0)
42     {
43         strcpy(this->name,name);
44     }
45     size = strlen(name);
46 }
47 //复制构造函数
48 Name::Name(Name& name)
49 {
50     cout << "copying\t " << name.name << endl;
51     this->name = new char[strlen(name.name)+1];
52     if(this->name != 0)
53     {
54         strcpy(this->name,name.name);
55     }
56     size = strlen(name.name);
57 }
58 //析构函数
59 Name::~Name()
60 {
61     cout << "distructing\t" << this->name<<endl;;
62     delete []name;
63     size = 0;
64 }
65 void main()
66 {
67     Name n1("zhangsan");
68     Name n2 = n1;
69     Name n3 = ("lisi");
70     n3 = n2 = n1;
71 }

 3.重载流插入和流提取运算符

 1 #include <cstdlib>
 2 #include <iostream>
 3 using namespace std;
 4 class Vector
 5 {
 6 public:
 7     Vector(int size=1);
 8     ~Vector();
 9     int& operator[](int i);
10     friend ostream& operator << (ostream& output,Vector& vector);
11     friend istream& operator >> (istream& input,Vector& vector);
12 private:
13     int* v;
14     int length;
15 };
16 Vector::Vector(int size)
17 {
18     if(size<=0||size>100)
19     {
20         cout << "The size of" << size << "is null!" << endl;
21     }else
22     {
23         v = new int[size];
24         length = size;
25     }
26 }
27 Vector::~Vector()
28 {
29     cout << "析构\n";
30     delete []v;
31     length = 0;
32 }
33 int& Vector::operator[](int i)
34 {
35     if(i>=0&&i<length)
36     {
37         return v[i];
38     }else
39     {
40         cout << "The subscript" << i << "is outside!" << endl;
41         exit(0);
42     }
43 }
44 ostream& operator <<(ostream& output,Vector& vector)
45 {
46     for(int i = 0;i < vector.length; i ++)
47     {
48         output << vector[i] << " ";
49     }
50     output << endl;
51     return output;
52 }
53 istream& operator >>(istream& input,Vector& vector)
54 {
55     for(int i=0;i<vector.length;i++)
56     {
57         input >> vector[i];
58     }
59     return input;
60 }
61 void main()
62 {
63     int k;
64     cout << "Input the length of Vector a:" << endl;
65     cin >> k;
66     Vector a(k);
67     cout <<"Input the elements of Vector a:" << endl;
68     //调用重载版本
69     cin >> a;
70     cout << "Output the elements of Vector a:" << endl;
71     //调用重载版本
72     cout << a;
73 }

 

posted @ 2015-03-10 22:47  溈鉨wo乄菰単  阅读(208)  评论(0编辑  收藏  举报