C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版-譚浩強)_第1頁
C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版-譚浩強)_第2頁
C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版-譚浩強)_第3頁
C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版-譚浩強)_第4頁
C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版-譚浩強)_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版—譚浩強)C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版—譚浩強)C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版—譚浩強)C++面向?qū)ο蟪绦蛟O(shè)計第六章課后習題答案(第2版—譚浩強)編制僅供參考審核批準生效日期地址:電話:傳真:郵編:第六章課后習題答案(第二版譚浩強)1://xt6-1/cpp#include<iostream>//如用VC++應(yīng)改為∶#include<>usingnamespacestd;//如用VC++應(yīng)取消此行#include""#include""#include""#include""intmain(){Cylindercy1,,,10);cout<<"\noriginalcylinder:\nx="<<()<<",y="<<()<<",r="<<()<<",h="<<()<<"\narea="<<()<<",volume="<<()<<endl;(15);;(5,5);cout<<"\nnewcylinder:\n"<<cy1;Point&pRef=cy1;cout<<"\npRefasapoint:"<<pRef;Circle&cRef=cy1;cout<<"\ncRefasaCircle:"<<cRef;return0;}3:解法一#include<iostream>usingnamespacestd;classPoint{public:Point(floata,floatb):x(a),y(b){}~Point(){cout<<"executingPointdestructor"<<endl;}private:floatx;floaty;};classCircle:publicPoint{public:Circle(floata,floatb,floatr):Point(a,b),radius(r){}~Circle(){cout<<"executingCircledestructor"<<endl;}private:floatradius;};intmain(){Point*p=newCircle,,;deletep;return0;}3:解法二#include<iostream>usingnamespacestd;classPoint{public:Point(floata,floatb):x(a),y(b){}~Point(){cout<<"executingPointdestructor"<<endl;}private:floatx;floaty;};classCircle:publicPoint{public:Circle(inta,intb,intr):Point(a,b),radius(r){}~Circle(){cout<<"executingCircledestructor"<<endl;}private:floatradius;};intmain(){Point*p=newCircle,,;Circle*pt=newCircle,,;deletept;return0;}3:解法三#include<iostream>usingnamespacestd;classPoint{public:Point(floata,floatb):x(a),y(b){}virtual~Point(){cout<<"executingPointdestructor"<<endl;}private:floatx;floaty;};classCircle:publicPoint{public:Circle(floata,floatb,floatr):Point(a,b),radius(r){}virtual~Circle(){cout<<"executingCircledestructor"<<endl;}private:floatradius;};voidmain(){Point*p=newCircle,,;deletep;}4:#include<iostream>usingnamespacestd;//定義抽象基類ShapeclassShape{public:virtualdoublearea()const=0;//純虛函數(shù)};//定義Circle類classCircle:publicShape{public:Circle(doubler):radius(r){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{return*radius*radius;};//定義虛函數(shù)protected:doubleradius;//半徑};//定義Rectangle類classRectangle:publicShape{public:Rectangle(doublew,doubleh):width(w),height(h){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{returnwidth*height;}//定義虛函數(shù)protected:doublewidth,height;//寬與高};classTriangle:publicShape{public:Triangle(doublew,doubleh):width(w),height(h){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{return*width*height;}//定義虛函數(shù)protected:doublewidth,height;//寬與高};//輸出面積的函數(shù)voidprintArea(constShape&s){cout<<()<<endl;}//輸出s的面積intmain(){Circlecircle;//建立Circle類對象circlecout<<"areaofcircle=";printArea(circle);//輸出circle的面積Rectanglerectangle,;//建立Rectangle類對象rectanglecout<<"areaofrectangle=";printArea(rectangle);//輸出rectangle的面積Triangletriangle,;//建立Triangle類對象cout<<"areaoftriangle=";printArea(triangle);//輸出triangle的面積return0;}5:#include<iostream>usingnamespacestd;//定義抽象基類ShapeclassShape{public:virtualdoublearea()const=0;//純虛函數(shù)};//定義Circle(圓形)類classCircle:publicShape{public:Circle(doubler):radius(r){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{return*radius*radius;};//定義虛函數(shù)protected:doubleradius;//半徑};//定義Square(正方形)類classSquare:publicShape{public:Square(doubles):side(s){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{returnside*side;}//定義虛函數(shù)protected:doubleside;};//定義Rectangle(矩形)類classRectangle:publicShape{public:Rectangle(doublew,doubleh):width(w),height(h){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{returnwidth*height;}//定義虛函數(shù)protected:doublewidth,height;//寬與高};//定義Trapezoid(梯形)類classTrapezoid:publicShape{public:Trapezoid(doublet,doubleb,doubleh):top(t),bottom(t),height(h){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{return*(top+bottom)*height;}//定義虛函數(shù)protected:doubletop,bottom,height;//上底、下底與高};//定義Triangle(三角形)類classTriangle:publicShape{public:Triangle(doublew,doubleh):width(w),height(h){}//結(jié)構(gòu)函數(shù)virtualdoublearea()const{return*width*height;}//定義虛函數(shù)protected:doublewidth,height;//寬與高};intmain(){Circlecircle;//建立Circle類對象circleSquaresquare;//建立Square類對象squareRectanglerectangle,;//建立Rectangle類對象rectangleTrapezoidtrapezoid,,;//建立Trapezoid類對象trapezoidTriangletriangle,;//建立Triangle類對象Shape*pt[5]={&circle,&square,&rectangle,&t

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論