c++重载运算符梳理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++重载运算符梳理相关的知识,希望对你有一定的参考价值。

 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++重载运算符梳理的主要内容,如果未能解决你的问题,请参考以下文章

C++提高:运算符重载

运算符 + 重载 C++

C++运算符重载

c++ 运算符重载问题

C++重载'--'后缀运算符

C++ 运算符重载