C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx
《C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx(9页珍藏版)》请在冰豆网上搜索。
![C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx](https://file1.bdocx.com/fileroot1/2023-4/17/e5ecfaa8-dd15-4475-bca2-974d3b4bda01/e5ecfaa8-dd15-4475-bca2-974d3b4bda011.gif)
C++面向对象程序设计第六章课后习题答案第2版谭浩强
第六章课后习题答案(第二版谭浩强)
1:
//xt6—1/cpp
#include〈iostream〉//如用VC++应改为∶#includeh〉
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;
}