c++重载运算符@
来源:互联网 发布:下载软件管家360 编辑:程序博客网 时间:2024/05/20 10:52
c++重载运算符@
刚刚将c++的重载运算符学完,虽然也不是挺会,但也能有小小心得吧!
重载运算符有友元和静态两种方式使用吧!
友元:
class FeetInches{private:int feet; // 英尺int inch; // 英寸public:FeetInches(int f = 0, int i = 0)//构造函数{feet = f; inch = i;}friend FeetInches hanshu(FeetInches& a);};FeetInches hanshu(FeetInches& a){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()的函数调用
看了上面的例子是否觉得重载很有用呢?第一次打这么多的字,累了。看完上面的的解析剩下的重载应该看的懂了吧!
完整的代码点这里
1 0
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- C++:运算符重载
- C#:运算符重载
- C++:运算符重载
- cocos2d-x3.1.1schedule_selector类型转换无效
- spring4.0源码下载
- Eclipse + Android + 真机调试的问题
- Java Swing 中插入图片背景
- 网页元素居中攻略记_(1)元素水平居中
- c++重载运算符@
- 对C++中迭代器的分类的认识
- cocos2d-x 定时器selector的使用 :schedule的使用
- 3.1-1
- PHP 删除数组元素中的坑
- AJAX——UpdatePanel
- Could not find the main class: org.apache.catalina.startup.Boostrap. Program will exit.
- Java synchronized 关于锁的对象顺序问题
- cocos2d-x的scheduleSelector