C/C++知识点:操作符重载案例
小标 2018-06-25 来源 : 阅读 1231 评论 0

摘要:本文主要向大家介绍了C/C++知识点的操作符重载案例,通过具体的代码向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点的操作符重载案例,通过具体的代码向大家展示,希望对大家学习C/C++知识点有所帮助。

1 案例:+ ,- ,++, --

[cpp] view plain copy
1. // 操作符重载.cpp : 定义控制台应用程序的入口点。  
2. //  
3.   
4. #include "stdafx.h"  
5.   
6.   
7. class Fraction  
8. {  
9. public:  
10.     Fraction(): num(2),den(1){  
11.         //默认值  
12.     }  
13.   
14.     Fraction(int n,int d):num(n),den(d){  
15.   
16.     }  
17.   
18.     ~Fraction();  
19.   
20.   
21.     Fraction operator + (const Fraction &other){  
22.         Fraction result;  
23.         result.den = den * other.den;//分母相乘  
24.         result.num = num * other.den + den * other.num;//分子交叉相乘  
25.         return result;  
26.     }  
27.   
28.       
29.   
30.     Fraction operator - (const Fraction &other){  
31.         Fraction result;  
32.         result.den = den * other.den;//分母相乘  
33.         result.num = num * other.den - den * other.num;//分子交叉相减  
34.         return result;  
35.     }  
36.   
37.     Fraction operator ++ (){  
38.         Fraction result;  
39.         result.den = den ;//分母不变  
40.         result.num = num + den;//分子加上分母  
41.         return result;  
42.     }  
43.   
44.     Fraction operator -- (){  
45.         Fraction result;  
46.         result.den = den ;//分母不变  
47.         result.num = num - den;//分子减去分母  
48.         return result;  
49.     }  
50.   
51. public:  
52.     int num;  
53.     int den;  
54.   
55. };  
56.   
57. Fraction::~Fraction()  
58. {  
59.   
60. }  
61.   
62.   
63. int _tmain(int argc, _TCHAR* argv[])  
64. {  
65.     Fraction *f1 = new Fraction(5,6);  
66.     Fraction f2(2,3);  
67.     Fraction f3(3,5);  
68.     Fraction f4 = f2 + f3;  
69.     Fraction f5 = f2 - f3;  
70.     Fraction f6 = f5--;  
71.     Fraction f7 = f5++;  
72.   
73.     return 0;  
74. }

 

2 案例: []

 1 简要说明: 

   创建2个类,Student和StudentData类,

   Student里有id,

   StudentData里使用操作符[]获取Student,然后输出id

  2 代码如下:

[cpp] view plain copy
1. // 重载2.cpp : 定义控制台应用程序的入口点。  
2. // [] 重载案例  
3.   
4. #include "stdafx.h"  
5. #include <stdio.h>  
6. #include <stdlib.h>  
7.   
8.   
9. class Student  
10. {  
11. public:  
12.     Student(){}  
13.     Student(int id):id(id){}  
14.   
15.     ~Student(){}  
16.   
17.     void showId(){  
18.         printf("student id:%d\n",id);  
19.     }  
20.   
21. private:  
22.     int id;   
23. };  
24.   
25.   
26. class StudentData  
27. {  
28. public:  
29.     StudentData(int size);  
30.     ~StudentData();  
31.   
32.     void addStudent(Student *stu){  
33.         *(_stu_buf + count ) = *stu;  
34.         count++;  
35.     }  
36.   
37.     Student operator[] (int id){  
38.         return _stu_buf[id];  
39.     }  
40. private:  
41.     int count;  
42.     Student *_stu_buf;  
43. };  
44.   
45. StudentData::StudentData(int size)  
46. {  
47.     count = 0;  
48.     _stu_buf = (Student *)malloc(size * sizeof(Student));   
49. }  
50.   
51. StudentData::~StudentData()  
52. {  
53.   
54. }  
55.   
56.   
57. int _tmain(int argc, _TCHAR* argv[])  
58. {  
59.     //定义student集合  
60.     StudentData data(10);  
61.   
62.     //批量创建student  
63.     for (int i = 0; i < 10; i++)  
64.     {  
65.         Student stu(i);  
66.         data.addStudent(&stu);  
67.     }  
68.   
69.     for (int i = 0; i < 10; i++)  
70.     {  
71.         // 显示id  
72.         data[i].showId();  
73.     }  
74.   
75.   
76.     while (true)  
77.     {  
78.   
79.     }  
80.   
81.     return 0;  
82. }

 

     3 效果如下

 C/C++知识点:操作符重载案例

本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程