四、綜合應(yīng)用題(18分)
使用VC++6.0打開考生文件夾下的源程序文件3.cpp,要求編寫一個(gè)CMyShape類,含有求面積、周長(zhǎng)等的純虛函數(shù),然后編寫一個(gè)CRectangle類和CCircle類繼承CMyShape,并實(shí)現(xiàn)求面積、周長(zhǎng)的兩個(gè)函數(shù)。在main函數(shù)中測(cè)試得到下面的結(jié)果:
在CMyShape類構(gòu)造函數(shù)內(nèi)
在CCircle類構(gòu)造函數(shù)內(nèi)
在CMyShape類構(gòu)造函數(shù)內(nèi)
在CReetangle類構(gòu)造函數(shù)內(nèi)
myCircle:Area=314.159Girth=62.8319
myRectangle:Area=900Girth=120
具體要求如下:
(1)定義求面積的純虛函數(shù),請(qǐng)?jiān)谧⑨?后添加適當(dāng)?shù)恼Z句。
(2)定義求周長(zhǎng)的純虛函數(shù),請(qǐng)?jiān)谧⑨?后添加適當(dāng)?shù)恼Z句。
(3)請(qǐng)?jiān)谧⑨?和注釋4后添加適當(dāng)?shù)恼Z句。
注意:除在指定位置添加語句之外,不要改動(dòng)程序中的其他內(nèi)容。
試題程序:
#include
#inelude
#definePI3.1415926
classCMyPoint
{
public:
intX,y;
CMyPoint(inttx,intty):x(tx),y(ty){}
};
classCMyShape
{
public:
CMyShape(){cout{{"在CMyShape類構(gòu)造函數(shù)內(nèi)"
< //********1********
//********2********
protected:
};
classCCircle:publicCMyShape
{
public:
CCircle(CMyPointi,doublej):CMyShape(),arc-
Center(i),radius(j)
{
cout<<"在CCircle類構(gòu)造函數(shù)內(nèi)"< }
doubleGetArea()
{
returnPI*radius*radius:
}
doubleGetGirth()
{
return2*PI*radius:
}
private:
CMyPointarcCenter;
doubleradius;
};
classCRectangle:publicCMyShape
{
public:
CRectangle(CMyPointIt,CMyPomtrb):leftTop(it),
rightBottom(rb),CMyShape()
{
cout<<"在CRectangle類構(gòu)造函數(shù)內(nèi)"< }
doubleGetArea()
{
intwidth=abs(rightBottom.X-leftTop.x);
intheight=abs(rightBottom.y-leftTop.y);
returnwidth*height:
}
doubleGetGirth()
{
intwidth=abs(rightBottom.x-leftTop.x);
intheight=abs(rightBottom.y-leftTop.y);
return2*(width+height):
}
private:
CMyPointleftTop,rightBottom;
};
voidmain()
{
CMyShape*myShape=NULL;
CCircle*myCircle=newCCircle(CMyPoint(5,5),10);
CRectangle*myRectangle=newCRectangle(CMy—Point(0,O),CMyPoint(30,30));
//********3********
cout<<"myCircle:"<<"Area="<
![]() | ![]() .. 定價(jià):¥225 優(yōu)惠價(jià):¥213 更多書籍 |
![]() | ![]() .. 定價(jià):¥133 優(yōu)惠價(jià):¥133.0 更多書籍 |