




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、第六章課后習(xí)題答案(第二版譚浩強)1:/xt6-1/cpp#include <iostream> /如用VC+應(yīng)改為#include <>using namespace std; /如用VC+應(yīng)取消此行#include ""#include ""#include ""#include ""int main()Cylinder cy1,10); cout<<"noriginal cylinder:nx="<<()<<", y=&
2、quot;<<()<<", r="<<()<<", h="<<()<<"narea="<<()<<", volume="<<()<<endl; (15); ; (5,5); cout<<"nnew cylinder:n"<<cy1; Point &pRef=cy1; cout<<"npRef as a point:"
3、;<<pRef; Circle &cRef=cy1; cout<<"ncRef as a Circle:"<<cRef; 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; private: float x; float y
4、;class Circle:public Pointpublic: Circle(float a,float b,float r):Point(a,b),radius(r) Circle()cout<<"executing Circle destructor"<<endl; private: float radius;int main()Point *p=new Circle,; delete p; return 0;3:解法二#include <iostream>using namespace std;class Pointpublic
5、: 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(int a,int b,int r):Point(a,b),radius(r) Circle()cout<<"executing Circle destructor"<<endl; private: float r
6、adius;int main()Point *p=new Circle,; Circle *pt=new Circle,; delete pt; return 0;3:解法三#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 Circl
7、e:public Pointpublic:Circle(float a,float b,float r):Point(a,b),radius(r) virtual Circle()cout<<"executing Circle destructor"<<endl; private:float radius;void main()Point *p=new Circle,; delete p;4:#include <iostream>using namespace std;/定義抽象基類Shapeclass Shapepublic: virt
8、ual double area() const =0; /純虛函數(shù);/定義Circle類class Circle:public Shapepublic:Circle(double r):radius(r) /結(jié)構(gòu)函數(shù) virtual double area() const return *radius*radius; /定義虛函數(shù) protected: double radius; /半徑;/定義Rectangle類class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) /結(jié)構(gòu)函數(shù)
9、virtual double area() const return width*height; /定義虛函數(shù)protected: double width,height; /寬與高;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) /結(jié)構(gòu)函數(shù) virtual double area() const return *width*height; /定義虛函數(shù) protected: double width,height; /寬與高;/輸出面積的函數(shù)void printArea(con
10、st Shape &s)cout<<()<<endl; /輸出s的面積int main() Circle circle; /建立Circle類對象circle cout<<"area of circle =" printArea(circle); /輸出circle的面積 Rectangle rectangle,; /建立Rectangle類對象rectangle cout<<"area of rectangle =" printArea(rectangle); /輸出rectangle的面積 Tr
11、iangle triangle,; /建立Triangle類對象 cout<<"area of triangle =" printArea(triangle); /輸出triangle的面積 return 0;5:#include <iostream>using namespace std;/定義抽象基類Shapeclass Shapepublic: virtual double area() const =0; /純虛函數(shù);/定義Circle(圓形)類class Circle:public Shapepublic:Circle(double r):
12、radius(r) /結(jié)構(gòu)函數(shù) virtual double area() const return *radius*radius; /定義虛函數(shù) protected: double radius; /半徑;/定義Square(正方形)類class Square:public Shapepublic: Square(double s):side(s) /結(jié)構(gòu)函數(shù) virtual double area() const return side*side; /定義虛函數(shù) protected: double side;/定義Rectangle(矩形)類class Rectangle:public S
13、hapepublic: Rectangle(double w,double h):width(w),height(h) /結(jié)構(gòu)函數(shù) virtual double area() const return width*height; /定義虛函數(shù) protected: double width,height; /寬與高;/定義Trapezoid(梯形)類class Trapezoid:public Shapepublic: Trapezoid(double t,double b,double h):top(t),bottom(t),height(h) /結(jié)構(gòu)函數(shù) virtual double ar
14、ea() const return *(top+bottom)*height; /定義虛函數(shù) protected: double top,bottom,height; /上底、下底與高;/定義Triangle(三角形)類class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) /結(jié)構(gòu)函數(shù) virtual double area()const return *width*height; /定義虛函數(shù) protected: double width,height; /寬與高;int main() Circle circle; /建立Circle類對象circle Square square; /建立Square類對象square Rectangle rectangle,; /建立Rectangle類對象rectangle Trapezoid trapezoid,; /建立Trapezoid類對象trapezoid Triangle triangle,; /建立Triangle類對象 Shape *pt5=&circle,&square,&re
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025私營企業(yè)勞動合同管理規(guī)范
- 2025專業(yè)版小學(xué)教室的租賃合同樣本
- 2025汽車銷售合同格式范文
- 2025網(wǎng)絡(luò)主播經(jīng)紀(jì)合同范本
- 2025建筑陶瓷供應(yīng)合同范本 建筑陶瓷供應(yīng)合同模板
- 2025年上海市分體式空調(diào)安裝與維護(hù)合同樣本
- 《青少年財經(jīng)素養(yǎng)教育》課件
- 《紅樓夢綺夢》課件
- 杏色淡雅古風(fēng)國潮時令節(jié)氣端午節(jié)
- 2025年吉林貨運從業(yè)資格考試題目及答案詳解
- 無人機失控應(yīng)急事件處置預(yù)案
- 駐廠協(xié)議書模板
- 樹木清除合同協(xié)議
- 2024年韶關(guān)市始興縣事業(yè)單位招聘工作人員筆試真題
- 安徽省皖南八校2024-2025學(xué)年高一下學(xué)期4月期中考試數(shù)學(xué)試題
- 國家發(fā)展改革委低空經(jīng)濟(jì)司
- 單位體檢協(xié)議書模板合同
- 委托律師簽署協(xié)議書
- 圖文工廠轉(zhuǎn)讓協(xié)議書
- 貨物貿(mào)易的居間合同
- 2025-2030中國療養(yǎng)院行業(yè)市場深度分析及前景趨勢與投資研究報告
評論
0/150
提交評論