


版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、作業81. 聲明一個基類Point(點), 有私有成員x,y為其坐標。在Point類上公有派生出Circle(圓)類和Rectangle(長方形)類。Circle類以Point基類點為圓心,并有自己的私有成員半徑R。Rectangle類以Point基類點為矩形左下角點的坐標,并有自己的私有成員高Height和寬Width。這三個類都有構造函數、復制構造函數、析構函數、計算當前對象面積的GetArea( )函數等成員函數。再使用Rectangle類創建一個派生類Square(正方形),為Square類編寫構造函數、復制構造函數、析構函數、計算當前對象面積的GetArea( )函數等成員函數。并設
2、計創建各種類的對象,調用所有函數。參照例題,設計函數f(Point &a)能對不同對象的實參調用計算打印出相應對象的面積,再用基類Point的指針數組指向4個不同類的動態對象,并調用打印出相應對象的面積。(練習使用虛函數)程序實現:/*2015.5.10第八次作業 第一題1. 聲明一個基類Point(點), 有私有成員x,y為其坐標。在Point類上公有派生出Circle(圓)類和Rectangle(長方形)類。Circle類以Point基類點為圓心,并有自己的私有成員半徑R。Rectangle類以Point基類點為矩形左下角點的坐標,并有自己的私有成員高Height和寬Width。這三個類都
3、有構造函數、復制構造函數、析構函數、計算當前對象面積的GetArea( )函數等成員函數。再使用Rectangle類創建一個派生類Square(正方形),為Square類編寫構造函數、復制構造函數、析構函數、計算當前對象面積的GetArea( )函數等成員函數。并設計創建各種類的對象,調用所有函數。參照例題,設計函數f(Point &a)能對不同對象的實參調用計算打印出相應對象的面積,再用基類Point的指針數組指向4個不同類的動態對象,并調用打印出相應對象的面積。(練習使用虛函數)觀察到如果面積后面不加const,p-GetArea()全部是0 = =為何?by:haldak*/#inclu
4、de #include using namespace std;const double pi = 3.14159265;class Pointprivate:double x , y;public:Point(double a = 0 , double b= 0): x(a) , y(b)Point(const Point &p): x(p.x) , y(p.y)Point()coutT1endl;virtual double GetArea() const;double GetX();double GetY();inline double Point:GetX()return x;inli
5、ne double Point:GetY()return y;inline double Point:GetArea() constreturn 0.0;class Circle: public Pointprivate:double R;public:Circle(double r = 0 , double a = 0 , double b = 0): R(r) , Point(a,b)Circle(const Circle &p): R(p.R) , Point(p)Circle()coutT2endl;virtual double GetArea() const;double GetR(
6、);inline double Circle:GetArea()const return pi * R * R;inline double Circle:GetR()return R;class Rectangle: public Pointprivate:double Height , Width;public:Rectangle(double a = 0 , double b = 0 , double h = 0 , double w = 0): Point(a,b) , Height(h) , Width(w)Rectangle(const Rectangle &p): Point(p)
7、 , Height(p.Height) , Width(p.Width)Rectangle()coutT3endl;virtual double GetArea() const;double GetH();double GetW();inline double Rectangle:GetArea() const return Height * Width;inline double Rectangle:GetH()return Height;inline double Rectangle:GetW()return Width;class Square: public Rectanglepubl
8、ic:Square(double length = 0 , double a = 0 , double b = 0): Rectangle(a,b,length,length)Square(Square &p): Rectangle(p)Square()coutT4endl;virtual double GetArea();inline double Square:GetArea()return Rectangle:GetH() * Rectangle:GetW();int main()Point a( 1.0 , 1.0 );Circle b( 1.0 , 1.0 , 1.0 );Recta
9、ngle c( 1.0 , 1.0 , 1.0 , 2.0 );Square d(3.0 , 1.0 , 1.0 );couta.GetArea()endl;coutb.GetArea()endl;coutc.GetArea()endl;coutd.GetArea()endl;coutendl;Point *p4;p0 = new Point( 1.0 , 1.0 );p1 = new Circle( 1.0 , 1.0 , 1.0 );p2 = new Rectangle( 1.0 , 1.0 , 1.0 , 2.0 );p3 = new Square( 3.0 , 1.0 , 1.0 );
10、/Point *p4 = &a , &b , &c , &d ;for(int i = 0 ; i 4 ; i+)cout GetArea()endl;delete pi;return 0;運行結果:03.14159290T13.14159T12T19T1T4T3T1T3T1T2T1T1請按任意鍵繼續. . .2. 將上題中各個類的析構函數改為虛析構函數,其中打印出信息表示相應析構函數被調用。再用基類Point的指針數組指向不同的new動態對象,調用打印出相應對象的面積,并用delete刪除基類Point指針數組所指的不同動態對象,觀察是否調用了相應正確的析構函數。(練習使用虛析構函數)程序實
11、現:/*2015.5.10第八次作業 第二題2. 將上題中各個類的析構函數改為虛析構函數,其中打印出信息表示相應析構函數被調用。再用基類Point的指針數組指向不同的new動態對象,調用打印出相應對象的面積,并用delete刪除基類Point指針數組所指的不同動態對象,觀察是否調用了相應正確的析構函數。(練習使用虛析構函數)by:haldak*/#include #include using namespace std;const double pi = 3.14159265;class Pointprivate:double x , y;public:Point(double a = 0 ,
12、 double b= 0): x(a) , y(b)Point(const Point &p): x(p.x) , y(p.y)virtual Point()coutT1endl;virtual double GetArea() const;double GetX();double GetY();inline double Point:GetX()return x;inline double Point:GetY()return y;inline double Point:GetArea() constreturn 0.0;class Circle: public Pointprivate:d
13、ouble R;public:Circle(double r = 0 , double a = 0 , double b = 0): R(r) , Point(a,b)Circle(const Circle &p): R(p.R) , Point(p)virtual Circle()coutT2endl;virtual double GetArea() const;double GetR();inline double Circle:GetArea()const return pi * R * R;inline double Circle:GetR()return R;class Rectan
14、gle: public Pointprivate:double Height , Width;public:Rectangle(double a = 0 , double b = 0 , double h = 0 , double w = 0): Point(a,b) , Height(h) , Width(w)Rectangle(const Rectangle &p): Point(p) , Height(p.Height) , Width(p.Width)virtual Rectangle()coutT3endl;virtual double GetArea() const;double
15、GetH();double GetW();inline double Rectangle:GetArea() const return Height * Width;inline double Rectangle:GetH()return Height;inline double Rectangle:GetW()return Width;class Square: public Rectanglepublic:Square(double length = 0 , double a = 0 , double b = 0): Rectangle(a,b,length,length)Square(S
16、quare &p): Rectangle(p)virtual Square()coutT4endl;virtual double GetArea();inline double Square:GetArea()return Rectangle:GetH() * Rectangle:GetW();int main()/*Point a( 1.0 , 1.0 );Circle b( 1.0 , 1.0 , 1.0 );Rectangle c( 1.0 , 1.0 , 1.0 , 2.0 );Square d(3.0 , 1.0 , 1.0 );couta.GetArea()endl;coutb.G
17、etArea()endl;coutc.GetArea()endl;coutd.GetArea()endl;coutendl;*/Point *p4;p0 = new Point( 1.0 , 1.0 );p1 = new Circle( 1.0 , 1.0 , 1.0 );p2 = new Rectangle( 1.0 , 1.0 , 1.0 , 2.0 );p3 = new Square( 3.0 , 1.0 , 1.0 );/Point *p4 = &a , &b , &c , &d ;for(int i = 0 ; i 4 ; i+)cout GetArea()endl;delete pi;return 0;運行結果:. 0T13.14159T2T12T3T19T4T3T1請按任意鍵繼續. . .3*. 聲明一個基類Point(點),有私有成員x,y為其坐標。在Point類上公有派生Circle(圓)類和Square(正方形)類,Circle類以Point基類點為圓心,并有自己的私有成員半徑R。Square類以Point基類點為中心點,并有自己的私有成員邊長L。這三個類都有構造函數、復制構造函數、析構函數、計
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 含氟丙烯基樹脂及單體項目可行性研究報告(范文參考)
- Unit 3 My hometown Grammar教學設計2024-2025學年譯林版(2024)英語七年級下冊
- 2025年養老護理員專業知識測試卷:心理健康護理與溝通技巧測試
- Unit 1 How do we feel 第二課時(教學設計)-2024-2025學年滬教版(2024)三年級英語上冊
- 2025年消防執業資格考試題庫-消防應急救援預案編制與演練評估報告分析策略應用試題
- 2025年德語TestDaF考試模擬試卷:德語閱讀理解與寫作能力提高策略
- 2025年注冊會計師考試《會計》財務報表分析考點精講與模擬試題
- 成人高考語文易錯知識點專項訓練試題2025
- 2025年小學語文畢業升學考試全真模擬卷(文學名著閱讀)-《駱駝祥子》中的命運思考
- 2025年德語TestDaF考試模擬試卷:德語TestDaF考試口語表達訓練與試題
- 2024屆山西省重點中學中考適應性考試化學試題含解析
- 通止規設計公差自動計算表
- 靜設備安裝質量控制過程
- 國企74個風險點防控手冊
- 廣西忻城縣水良石灰巖礦開采項目環評報告
- 中國革命在探索中曲折前進
- 防排煙防火包裹施工方案
- 《大學生軍事理論教程》第五章
- 醫藥企業政府事務崗位職責
- 西裝基礎知識-課件
- 心得體會:好課“八要”
評論
0/150
提交評論