c++重载运算符@
时间:2015-05-30 09:24:27
收藏:0
阅读:356
c++重载运算符@
刚刚将c++的重载运算符学完,虽然也不是挺会,但也能有小小心得吧!

重载运算符有友元和静态两种方式使用吧!
友元:
class FeetInches { private: int feet; // 英尺 int inch; // 英寸 public: FeetInches(int f = 0, int i = 0)//构造函数 { feet = f; inch = i; } friend FeetInches hanshu(<span style="font-family: Arial, Helvetica, sans-serif;">FeetInches& a</span><span style="font-family: Arial, Helvetica, sans-serif;">);</span> }; FeetInches hanshu(<span style="font-family: Arial, Helvetica, sans-serif;">FeetInches& a</span><span style="font-family: Arial, Helvetica, sans-serif;">)</span> { cout << a.feet << " " << a.inch << endl; }
友元的意思是,一个类的朋友,它能够使用类里的任何元素,相当于没了权限一样。但是它只是朋友而已,所以并不要用FeetInches::hanshu()这样。这也是许多初学者较难想明白的地方吧!还有的是要注意友元是没有隐含的this指针的,所以你想要友元函数用class中的元素必须要有参数在()内,且要是类的类型,为什么一定要是类类型,若是不是要用class的元素我又何必要难为自己写个友元的东西,对吧?
静态:
class FeetInches { private: int feet; // 英尺 int inch; // 英寸 public: FeetInches(int f = 0, int i = 0)//构造函数 { feet = f; inch = i; } FeetInches hanshu(); }; void FeetInches::hanshu()//(1) { cout << feet << " " << inch << endl; cout << this->feet << " " << this->inch << endl; }
静态就是直接用,不要友元这种方式来表示。与友元不同的是它的函数(1)写在外面是与友元有很大区别的。它写在外面是要用FeetInches::(::为域的意思)来表示的,要表明它是FeetInches类内的函数。然而函数前面肯定有一个类型的,这里用一个void的类型来表示函数是无返回的类型,就是不用return。还有的是他有一个this指针隐藏起来了。this指的就是你(FeetInches a;a.hanshu();)a的地址值。说了这么多应该明白了吧!现在来谈谈重载了。
class FeetInches { private: int feet; // 英尺 int inch; // 英寸 public: FeetInches(int f = 0, int i = 0) { feet = f; inch = i; } FeetInches operator+(const FeetInches& a);//重载加法 friend ostream& operator<<(ostream& out, const FeetInches& a);//重载输出符 bool operator>(const FeetInches& a);//重载大于号 FeetInches operator*(const int a);//重载乘号 FeetInches& operator++();//重载先++ FeetInches operator++(int);//重载后++ }; FeetInches& FeetInches::operator++() { inch++; if (inch >= 12)feet += inch / 12, inch %= 12; return *this; } FeetInches FeetInches::operator++(int) { FeetInches temp=*this; inch++; if (inch >= 12)feet += inch / 12, inch %= 12; return temp; } bool FeetInches::operator>(const FeetInches& a) { if (feet > a.feet)return true; if (feet < a.feet)return false; return inch>a.inch ? true : false; } FeetInches FeetInches::operator+(const FeetInches& a) { feet += a.feet; inch += a.inch; return *this; } FeetInches FeetInches::operator*(const int a) { feet = a*feet; inch = a*inch; if (inch >= 12)feet += inch / 12, inch %= 12; return *this; } ostream& operator<<(ostream& out,const FeetInches& a) { out << a.feet << " " << a.inch << endl; return out; }
(2)
FeetInches a(2,4),b(6,8),c,d; cout << a + b << endl; d=c++; cout << c++ << " " << ++c << endl;
重载就是实现与常用的int的类型差不多,就是为了方便与美观而生的,感觉科学家们真伟大啊!
为什么要重载,因为一个类里面我要(2)里面的操作重载就是想int 一样一样的,int a=1+2;a++;一样多方便啊!
但是若不用重载,那我们就要自己写一个获取feet,inch的函数,因为这些元素都是私有的。而实现相加就每次都要写一个函数来调用它如(a+b):
FeetInches add(FeetInches& a,FeetInches& a) { <span style="white-space:pre"> </span>b.getfeet() += a.getfeet(); <span style="white-space:pre"> </span>b.getinch() += a.getinch(); <span style="white-space:pre"> </span>return *this;//上面说过 }//getfeet();getinch();为获取私有成员的函数 FeetInches a(1,2),b(3,4); add(a,b);//每次相加都要写add()的函数调用
看了上面的例子是否觉得重载很有用呢?第一次打这么多的字,累了。看完上面的的解析剩下的重载应该看的懂了吧!
原文:http://blog.csdn.net/zsc2014030403015/article/details/46241691
评论(0)