C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx

上传人:b****2 文档编号:2472375 上传时间:2023-05-03 格式:DOCX 页数:9 大小:16.47KB
下载 相关 举报
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第1页
第1页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第2页
第2页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第3页
第3页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第4页
第4页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第5页
第5页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第6页
第6页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第7页
第7页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第8页
第8页 / 共9页
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx_第9页
第9页 / 共9页
亲,该文档总共9页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx

《C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx(9页珍藏版)》请在冰点文库上搜索。

C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx

C++面向对象程序设计第六章课后习题答案第2版谭浩强

第六章课后习题答案(第二版谭浩强)

1:

//xt6—1/cpp

#include〈iostream〉//如用VC++应改为∶#include

h〉

usingnamespacestd;//如用VC++应取消此行

#include”cylinder.h"

#include"point.cpp"

#include"circle.cpp"

#include"cylinder。

cpp"

intmain()

{Cylindercy1(3。

5,6。

4,5.2,10);

cout<〈”\noriginalcylinder:

\nx=”<

getX()〈<",y="<〈cy1.getY()〈〈",r="

<〈cy1.getRadius()<〈",h="〈〈cy1.getHeight()〈〈”\narea="<〈cy1.area()

〈<",volume=”〈

volume()〈〈endl;

cy1.setHeight(15);

cy1。

setRadius(7。

5);

cy1.setPoint(5,5);

cout〈<"\nnewcylinder:

\n”〈〈cy1;

Point&pRef=cy1;

cout<<"\npRefasapoint:

"〈〈pRef;

Circle&cRef=cy1;

cout<<"\ncRefasaCircle:

”<

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(2.5,1.8,4。

5);

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"〈

private:

floatradius;

};

intmain()

{Point*p=newCircle(2.5,1.8,4.5);

Circle*pt=newCircle(2.5,1。

8,4。

5);

deletept;

return0;

}

3:

解法三

#include〈iostream〉

usingnamespacestd;

classPoint

{public:

Point(floata,floatb):

x(a),y(b){}

virtual~Point(){cout<<”executingPointdestructor”<

private:

floatx;

floaty;

};

classCircle:

publicPoint

{public:

Circle(floata,floatb,floatr):

Point(a,b),radius(r){}

virtual~Circle(){cout〈<”executingCircledestructor"〈

private:

floatradius;

};

voidmain()

{Point*p=newCircle(2。

5,1.8,4.5);

deletep;

}

4:

#include

usingnamespacestd;

//定义抽象基类Shape

classShape

{public:

virtualdoublearea()const=0;//纯虚函数

};

//定义Circle类

classCircle:

publicShape

{public:

Circle(doubler):

radius(r){}//结构函数

virtualdoublearea()const{return3.14159*radius*radius;};//定义虚函数

protected:

doubleradius;//半径

};

//定义Rectangle类

classRectangle:

publicShape

{public:

Rectangle(doublew,doubleh):

width(w),height(h){}//结构函数

virtualdoublearea()const{returnwidth*height;}//定义虚函数

protected:

doublewidth,height;//宽与高

};

classTriangle:

publicShape

{public:

Triangle(doublew,doubleh):

width(w),height(h){}//结构函数

virtualdoublearea()const{return0。

5*width*height;}//定义虚函数

protected:

doublewidth,height;//宽与高

};

//输出面积的函数

voidprintArea(constShape&s)

{cout〈

intmain()

Circlecircle(12。

6);//建立Circle类对象circle

cout<〈”areaofcircle=";

printArea(circle);//输出circle的面积

Rectanglerectangle(4。

5,8。

4);//建立Rectangle类对象rectangle

cout<〈"areaofrectangle=”;

printArea(rectangle);//输出rectangle的面积

Triangletriangle(4.5,8.4);//建立Triangle类对象

cout<<"areaoftriangle=”;

printArea(triangle);//输出triangle的面积

return0;

}

5:

#include〈iostream〉

usingnamespacestd;

//定义抽象基类Shape

classShape

{public:

virtualdoublearea()const=0;//纯虚函数

};

//定义Circle(圆形)类

classCircle:

publicShape

{public:

Circle(doubler):

radius(r){}//结构函数

virtualdoublearea()const{return3.14159*radius*radius;};//定义虚函数

protected:

doubleradius;//半径

};

//定义Square(正方形)类

classSquare:

publicShape

{public:

Square(doubles):

side(s){}//结构函数

virtualdoublearea()const{returnside*side;}//定义虚函数

protected:

doubleside;

};

//定义Rectangle(矩形)类

classRectangle:

publicShape

{public:

Rectangle(doublew,doubleh):

width(w),height(h){}//结构函数

virtualdoublearea()const{returnwidth*height;}//定义虚函数

protected:

doublewidth,height;//宽与高

};

//定义Trapezoid(梯形)类

classTrapezoid:

publicShape

{public:

Trapezoid(doublet,doubleb,doubleh):

top(t),bottom(t),height(h){}//结构函数

virtualdoublearea()const{return0.5*(top+bottom)*height;}//定义虚函数

protected:

doubletop,bottom,height;//上底、下底与高

};

//定义Triangle(三角形)类

classTriangle:

publicShape

{public:

Triangle(doublew,doubleh):

width(w),height(h){}//结构函数

virtualdoublearea()const{return0。

5*width*height;}//定义虚函数

protected:

doublewidth,height;//宽与高

};

intmain()

{

Circlecircle(12.6);//建立Circle类对象circle

Squaresquare(3。

5);//建立Square类对象square

Rectanglerectangle(4.5,8.4);//建立Rectangle类对象rectangle

Trapezoidtrapezoid(2.0,4。

5,3.2);//建立Trapezoid类对象trapezoid

Triangletriangle(4.5,8。

4);//建立Triangle类对象

Shape*pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};

//定义基类指针数组pt,使它每一个元素指向一个派生类对象

doubleareas=0.0;//areas为总面积

for(inti=0;i<5;i++)

{areas=areas+pt[i]-〉area();}

cout<<”totolofallareas=”〈

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2