码迷,mamicode.com
首页 > 编程语言 > 详细

c++重载运算符梳理

时间:2016-06-07 14:34:47      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include<iostream>
 2 using namespace std;
 3 class bign
 4 {
 5     public:int x;
 6             int y;
 7             int z;
 8     public:
 9            bign(int a,int b,int c)
10            {
11                x=a;
12                y=b;
13                z=c;
14            }//注意用友元函数
15            friend bign operator + (bign a,bign b);//重定义+
16            friend bign operator - (bign a,bign b);//重定义-
17            friend bign operator * (bign a,bign b);//重定义*
18            friend bign operator / (bign a,bign b);//重定义/
19            friend istream & operator >> (istream &in,bign &a);//重定义>>
20            friend ostream & operator << (ostream &out,bign &a);//重定义<<
21 };
22 bign operator + (bign a,bign b)
23 {
24     return bign(a.x+b.x,a.y+b.y,a.z+b.z);
25 }
26 bign operator - (bign a,bign b)
27 {
28     return bign(a.x-b.x,a.y-b.y,a.z-b.z);
29 }
30 bign operator * (bign a,bign b)
31 {
32     return bign(a.x*b.x,a.y*b.y,a.z*b.z);
33 }
34 bign operator / (bign a,bign b)
35 {
36     return bign(a.x/b.x,a.y/b.y,a.z/b.z);
37 }
38 istream & operator >> (istream &in,bign &a)
39 {
40     in>>a.x>>a.y>>a.z;
41     return in;
42 }
43 ostream & operator << (ostream &out,bign &a)
44 {
45     out<<a.x<<" "<<a.y<<" "<<a.z<<endl;
46     return out;
47 }
48 int main() 
49 {
50     bign x(0,0,0),y(90,100,908),z(0,0,0);
51     cin>>x;
52     z=x+y;
53     cout<<z<<endl;
54     z=x-y;
55     cout<<z<<endl;
56     z=x*y;
57     cout<<z<<endl;
58     z=y/x;
59     cout<<z<<endl;
60     system("pause");
61     return 0;
62 }

 

c++重载运算符

  ---by ysmor

重新解释运算符的含义,叫做运算符重载-----c++程序设计P277

不多说了,给代码

 

输入9 100 2

运行结果

99 200 910

-81 0 -906

810 10000 1816

10 1 454

请按任意键继续. . .

THE END...

欢迎大家跟帖,并继续关注我

注意:本博客为原创作品,csdn上的那篇也是我发的

链接http://blog.csdn.net/yigezhongxuesheng/article/details/51602077

 

c++重载运算符梳理

标签:

原文地址:http://www.cnblogs.com/ysmor/p/5566654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!