C++面向對象程序設計第六章課后習習題答案(第2版—譚浩強)_第1頁
C++面向對象程序設計第六章課后習習題答案(第2版—譚浩強)_第2頁
C++面向對象程序設計第六章課后習習題答案(第2版—譚浩強)_第3頁
C++面向對象程序設計第六章課后習習題答案(第2版—譚浩強)_第4頁
C++面向對象程序設計第六章課后習習題答案(第2版—譚浩強)_第5頁
已閱讀5頁,還剩5頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、第六章課后習題答案(第二版譚浩強)1:/xt6-1/cpp#include <iostream> /如用VC+應改為#include <>using namespace std; /如用VC+應取消此行#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; /純虛函數;/定義Circle類class Circle:public Shapepublic:Circle(double r):radius(r) /結構函數 virtual double area() const return *radius*radius; /定義虛函數 protected: double radius; /半徑;/定義Rectangle類class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) /結構函數

9、virtual double area() const return width*height; /定義虛函數protected: double width,height; /寬與高;class Triangle:public Shapepublic: Triangle(double w,double h):width(w),height(h) /結構函數 virtual double area() const return *width*height; /定義虛函數 protected: double width,height; /寬與高;/輸出面積的函數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; /純虛函數;/定義Circle(圓形)類class Circle:public Shapepublic:Circle(double r):

12、radius(r) /結構函數 virtual double area() const return *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 S

13、hapepublic: Rectangle(double 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 ar

14、ea() const return *(top+bottom)*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 *width*height; /定義虛函數 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論