程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> c++重載運算符梳理,重載運算符梳理

c++重載運算符梳理,重載運算符梳理

編輯:C++入門知識

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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved