靜態數據成員與子對象_第1頁
靜態數據成員與子對象_第2頁
靜態數據成員與子對象_第3頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、/*定義一個圓類,聲明一個靜態數據成員和一個靜態成員函數,靜態數據成員表示圓的個數,定義對象時個數加1,西狗屎個數減1,靜態成員函數顯示靜態數據成員的值*/#include <iostream>using namespace std;class Circlepublic:Circle(float tr) r=tr ;countP+; Circle(Circle &p)r=p.r;countP+; static void GetP( ) cout<<" circle num="<<countP<<endl; Circle

2、()countP-; private:int r;static int countP;int Circle:countP=0; /給靜態數據成員countP初始化void main( )Circle A(4); A.GetP(); /*模擬商店一種貨物的購進和賣出情況,要求該種貨物以重量為單位成箱買賣,每箱的重量不相同,并且要求記錄目前庫存總重量和總價格。*/#include <iostream>using namespace std;class Goodspublic:Goods(float tprice,float tweight) unitPrice=tprice ; wei

3、ght=tweight;sumPrice+=unitPrice*weight;sumWeight+=weight;selFlag=0; void sell() selFlag=1; sumWeight-=weight;sumPrice-=unitPrice*weight;static void Show( )cout<<" sumPrice="<<sumPrice<<endl<<" sumWeight="<<sumWeight<<endl; private:float unitPri

4、ce,weight,selFlag;static float sumPrice,sumWeight;float Goods:sumPrice =0;float Goods:sumWeight=0; void main( )Goods A(4,5); A.Show (); A.sell ();A.Show(); /定義點類,并用兩個點構成一個矩形,求矩形的面積和周長#include<iostream>#include<cmath>using namespace std;class Point/Point類定義public:Point(int xx=0, int yy=0)

5、 X=xx;Y=yy;Point(Point &p);int GetX() return X;int GetY() return Y;friend class Rectangle;private:int X,Y;Point:Point(Point &p)/拷貝構造函數的實現X=p.X;Y=p.Y;/類的聚集class Rectangle/Distance類的定義public:/外部接口Rectangle(Point xp1, Point xp2);Rectangle(int x1,int y1,int x2,int y2);double Area() return length

6、*width;double RoundLength()return 2*(length+width);private:/私有數據成員Point p1,p2;/Point類的對象p1,p2double length,width;Rectangle:Rectangle(Point xp1, Point xp2):p1(xp1),p2(xp2) /類的聚集的構造函數length=abs(p1.X-p2.X);width=abs(p1.Y -p2.Y);Rectangle:Rectangle(int x1,int y1,int x2,int y2):p1(x1,y1),p2(x2,y2) length=abs(p1.X-p2.X);width=abs(p1.Y -p2.Y);/主函數void main()int x1,x2,y1,y2;cin>>x1>>y1>>x2>>y2;Point myp1(x1,y1),myp2(x2,y2);/定義Point類的對象Rectangle r1(myp1,myp2);/定義Distance類的對象cout<<"The Rectangel's area is: "<<r1.Area()<<endl; R

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論