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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 玩完游戏想吐怎么办 玩完了海盗船想吐怎么办 戴眼镜恶心想吐怎么办 玩电脑恶心想吐怎么办 玩游戏玩的头疼怎么办 玩游戏头疼想吐怎么办 游戏玩久了头疼怎么办 有3d眩晕症怎么办 玩游戏晕3d怎么办 梦幻西游亏的钱怎么办 普惠卡销户了钱存进去了怎么办 梦幻西游现金变储备了怎么办 孩子挣了钱存不下怎么办 电脑显示副本不是正版怎么办 斗战神师徒一个人删除角色怎么办 起业kx5防盗器不响怎么办 灌浆记录仪存盘满了怎么办 自首后发现无罪证据怎么办 中国劲酒过期了怎么办 玻尿酸流到眼皮怎么办 手指被胶带缠紫了怎么办 打玻尿酸不平整怎么办 孩子被老师体罚我该怎么办 孩子妈妈入狱了我该怎么办 机顶盒电视收不到台怎么办 跳芭蕾舞下面硬起来了怎么办 深情密码结局赵深深怎么办 宝宝头着地摔了怎么办 小孩头着地摔了怎么办 头朝下墩了脖子怎么办 两岁摔倒头着地怎么办 马桶大便冲不下去怎么办 脚丫吧里起泡痒怎么办 脚受伤后肿了怎么办 摔到了腿受伤了怎么办 骑车摔倒了肿了怎么办 生活大冒险老婆失踪了怎么办 手机qq图裂了怎么办 qq发的图裂了怎么办 抖音账号搬运多怎么办 别人搜不到我QQ怎么办