应用题
1. 使用VC++2010打开考生文件夹下“proj2”文件夹中的工程proj2.sln,要求编写一个CMyShape类,含有求面积求周长等纯虚函数。然后编写一个CMyRectangle类和CMyCircle类继承CMyShape,并实现求面积、求周长的两个函数。在main()函数中测试得到下面的结果:
在CMyShape类构函数造内
在CMyCircle类构造函数内
在CMyShape类构造函数内
在CMyRectangle类构造函数内
myCircle:Area=314.159 Girth=62.8319
myRectangle:Area=900 Girth=120
具体要求如下:
(1)定义求面积纯虚函数,请在注释//********1********之处添加适当的语句。
(2)定义求周长纯虚函数,请在注释//********2********之处添加适当的语句。
(3)请在注释//********3********和//********4********之处添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include <iostream.h>
#include <math.h>
#define PI 3.1415926
class CMyPoint
{
public:
int x,y;
CMyPoint(int tx,int ty):
x(tx),y(ty){}
};
class CMyShape
{
public:
CMyShape() {cout<<"在CMyShape类构造函数内"<<endl;}
//********1********
//********2********
protected:
};
class CMyCircle: public CmyShape
{
public:
CMyCircle(CMyPoint i,double j):CMyShape(),arcCenter(i), radius(j)(
cout<<"在 CMyCircle类构造函数内"<<endl;
}
double GetArea()
{
return PI*radius*radius;
}
double GetGirth()
{
return 2*PI*radius;
}
private:
CMyPoint arcCenter;
double radius;
};
class CMyRectangle: public CmyShape
{
public:
CMyRectangle(CMyPoint lt,CMyPoint rb):leftTop(lt), rightBottom(rb),CMyShape() {
cout<<"在 CMyRectangle类构造函数内"<<endl;
}
Double GetArea()
{
int width=abs (rightBottom.x-leftTop.x);
int height=abs (rightBottom.y-leftTop.y);
Return width*height;
}
double GetGirth()
{
int width=abs (rightBottom.x-leftTop.x);
int height=abs (rightBottom.y-leftTop.y);
return 2*(width+height);
}
private:
CMyPoint leftTop, rightBottom;
};
void main()
{
CMyShape *myShape=NULL;
CMyCircle *myCircle=new CMyCircle(CMyPoint(5,5),10);
CMyRectangle *myRectangle =new CMyRectangle(CMyPoint(0,0), CMyPoint(30,30));
//********3********
cout<<"myCircle:"<<"Area=" <<myShape->GetArea () <<"\t"
<<" Girth="<<myShape-> GetGirth () <<endl;
//********4********
cout<<"myRectangle: "<<"Are a= "<<myShape->Ge tArea () <<" \t"
<<"Girth="<<my Shape-> GetGirth()<<endl;
}