![C++面向對象程序設計第六章課后習題答案(第2版—譚浩強)_第1頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/5481cda5-3e7a-4398-9ad6-dc56c8625d3d/5481cda5-3e7a-4398-9ad6-dc56c8625d3d1.gif)
![C++面向對象程序設計第六章課后習題答案(第2版—譚浩強)_第2頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/5481cda5-3e7a-4398-9ad6-dc56c8625d3d/5481cda5-3e7a-4398-9ad6-dc56c8625d3d2.gif)
![C++面向對象程序設計第六章課后習題答案(第2版—譚浩強)_第3頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/5481cda5-3e7a-4398-9ad6-dc56c8625d3d/5481cda5-3e7a-4398-9ad6-dc56c8625d3d3.gif)
![C++面向對象程序設計第六章課后習題答案(第2版—譚浩強)_第4頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/5481cda5-3e7a-4398-9ad6-dc56c8625d3d/5481cda5-3e7a-4398-9ad6-dc56c8625d3d4.gif)
![C++面向對象程序設計第六章課后習題答案(第2版—譚浩強)_第5頁](http://file3.renrendoc.com/fileroot_temp3/2022-3/5/5481cda5-3e7a-4398-9ad6-dc56c8625d3d/5481cda5-3e7a-4398-9ad6-dc56c8625d3d5.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、精選優(yōu)質文檔-傾情為你奉上第六章課后習題答案(第二版譚浩強)1:/xt6-1/cpp#include <iostream> /如用VC+應改為#include <iosttram.h>using namespace std; /如用VC+應取消此行#include "cylinder.h"#include "point.cpp"#include "circle.cpp"#include "cylinder.cpp"int main()Cylinder cy1(3.5,6.4,5.2,10);
2、 cout<<"noriginal cylinder:nx="<<cy1.getX()<<", y="<<cy1.getY()<<", r="<<cy1.getRadius()<<", h="<<cy1.getHeight()<<"narea="<<cy1.area()<<", volume="<<cy1.volume()<&l
3、t;endl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); cout<<"nnew cylinder:n"<<cy1; Point &pRef=cy1; cout<<"npRef as a point:"<<pRef; Circle &cRef=cy1; cout<<"ncRef as a Circle:"<<cRef; return 0;3:解法一#include <i
4、ostream>using namespace std;class Pointpublic: Point(float a,float b):x(a),y(b) Point()cout<<"executing Point destructor"<<endl; private: float x; float y;class Circle:public Pointpublic: Circle(float a,float b,float r):Point(a,b),radius(r) Circle()cout<<"executin
5、g Circle destructor"<<endl; private: float radius;int main()Point *p=new Circle(2.5,1.8,4.5); delete p; return 0;3:解法二#include <iostream>using namespace std;class Pointpublic: Point(float a,float b):x(a),y(b) Point()cout<<"executing Point destructor"<<endl; pri
6、vate: float x; float y;class Circle:public Pointpublic:Circle(int a,int b,int r):Point(a,b),radius(r) Circle()cout<<"executing Circle destructor"<<endl; private: float radius;int main()Point *p=new Circle(2.5,1.8,4.5); Circle *pt=new Circle(2.5,1.8,4.5); delete pt; return 0;3:解
7、法三#include <iostream>using namespace std;class Pointpublic: Point(float a,float b):x(a),y(b) virtual Point()cout<<"executing Point destructor"<<endl; private: float x; float y;class Circle:public Pointpublic:Circle(float a,float b,float r):Point(a,b),radius(r) virtual Cir
8、cle()cout<<"executing Circle destructor"<<endl; private:float radius;void main()Point *p=new Circle(2.5,1.8,4.5); delete p;4:#include <iostream>using namespace std;/定義抽象基類Shapeclass Shapepublic: virtual double area() const =0; /純虛函數;/定義Circle類class Circle:public Shapepubl
9、ic:Circle(double r):radius(r) /結構函數 virtual double area() const return 3.14159*radius*radius; /定義虛函數 protected: double radius; /半徑;/定義Rectangle類class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) /結構函數 virtual double area() const return width*height; /定義虛函數protected:
10、double width,height; /寬與高;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) /結構函數 virtual double area() const return 0.5*width*height; /定義虛函數 protected: double width,height; /寬與高;/輸出面積的函數void printArea(const Shape &s)cout<<s.area()<<endl; /輸出s的面積int ma
11、in() Circle circle(12.6); /建立Circle類對象circle cout<<"area of circle =" printArea(circle); /輸出circle的面積 Rectangle rectangle(4.5,8.4); /建立Rectangle類對象rectangle cout<<"area of rectangle =" printArea(rectangle); /輸出rectangle的面積 Triangle triangle(4.5,8.4); /建立Triangle類對象 co
12、ut<<"area of triangle =" printArea(triangle); /輸出triangle的面積 return 0;5:#include <iostream>using namespace std;/定義抽象基類Shapeclass Shapepublic: virtual double area() const =0; /純虛函數;/定義Circle(圓形)類class Circle:public Shapepublic:Circle(double r):radius(r) /結構函數 virtual double area
13、() const return 3.14159*radius*radius; /定義虛函數 protected: double radius; /半徑;/定義Square(正方形)類class Square:public Shapepublic: Square(double s):side(s) /結構函數 virtual double area() const return side*side; /定義虛函數 protected: double side;/定義Rectangle(矩形)類class Rectangle:public Shapepublic: Rectangle(double
14、 w,double h):width(w),height(h) /結構函數 virtual double area() const return width*height; /定義虛函數 protected: double width,height; /寬與高;/定義Trapezoid(梯形)類class Trapezoid:public Shapepublic: Trapezoid(double t,double b,double h):top(t),bottom(t),height(h) /結構函數 virtual double area() const return 0.5*(top+b
15、ottom)*height; /定義虛函數 protected: double top,bottom,height; /上底、下底與高;/定義Triangle(三角形)類class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) /結構函數 virtual double area()const return 0.5*width*height; /定義虛函數 protected: double width,height; /寬與高;int main() Circle circle(12.6); /建立Circle類對象circle Square square(3.5); /建立Square類對象square Rectangle rectangle(4.5,8.4); /建立Rectangle類對象rectangle Trapezoid trapezoid(2.0,4.5,3.2); /建立Trapezoid類對象trapezoid Triangle triangle(4.5,8.4); /建立Triangle類對象 Shape *pt5=&circle,&square
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年企業(yè)機器質押借款合同
- 2025年勞動解除合同標準條款
- 2025年抗瘧藥項目申請報告模范
- 2025年貨車租賃與運輸服務合同樣本
- 2025年國際貨物買賣合同與慣例
- 2025年專業(yè)清潔人員派遣協議
- 2025年二手車購買合同范本
- 2025年三板市場股權買賣協議
- 2025年伙伴開設教育機構合作協議書模板
- 2025年繼電器研發(fā)策劃技術協議書范本
- JCT796-2013 回彈儀評定燒結普通磚強度等級的方法
- 懸挑腳手架搭設要求
- 幼兒園衛(wèi)生保健十三種表格
- 勞動用工備案表
- 業(yè)務提成獎勵方案
- 四年級語文上冊第一單元單元整體教學設計
- 玩具安全標準測試培訓-(SGS)課件
- 員工工資條模板
- 病例報告表格模板CRF
- 火力發(fā)電廠節(jié)能管理制度實施細則
- 《極致挑逗:雙人共撫全圖解120招》讀書筆記模板
評論
0/150
提交評論