使用VC6打开考生文件夹下的源程序文件modi3.cpp,要求编写一个CMyShape类,含有求面积求周长等纯虚函数。然后编写一个CMyRectangle类和CMyCircle类继承CMyShape,并实现求面积、求周长的两个函数。在main()i~数中测

admin2017-09-20  23

问题 使用VC6打开考生文件夹下的源程序文件modi3.cpp,要求编写一个CMyShape类,含有求面积求周长等纯虚函数。然后编写一个CMyRectangle类和CMyCircle类继承CMyShape,并实现求面积、求周长的两个函数。在main()i~数中测试得到下面的结果:
    在CMyShape类构函数造内
    在CMyCircle类构造函数内
    在CMyShape类构造函数内
    在CMyRectangle类构造函数内
    myCircle:Area=3 14.1 59    Girth=62.83 19
    myRectangle:Area==900 Girth==120
  具体要求如下:
(1)定义求面积纯虚函数,请在注释∥********1********之处添加适当的语句。
(2)定义求周长纯虚函数,请在注释∥********2********之处添加适当的语句。
(3)请在注释∥********3********和∥********4********之处添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
#include
#include
#define PI    3.1415926
clas S CMyPoint
{
public:
    int x,y;
    CMyPoint(int tx,int ty):
X(tx),Y(ty)f}
  };
class CMyShape
{
public:
    CMyShape(){cout<<“在CMyShape类构造函数内”<    ∥********1********
    ∥********2********
protected:
};
class CMyCircle:public CmyShape
{
public:
    CMyCircle(CMyPoint i,double
j):CMyShape(),arcCenter(i),
radius(j){
    cout<<“在CMyCircle类构造函数内”<    }
    double GetArea()
    {
    return PI*radius*radius ;
    }
    double GetGirth()
    {
    return 2*PI*radius;
    }
private:
    CMyPoint arcCenter;
    double radius;
  };
  class CMyRectangle:public
  CmyShape
  {
  public:
    CMyRectangle(CMyPoint
it,CMyPoint  rb):leftTop(it),
rightBottom(rb),CMyShape(){
    cout<<“在CMyRectangle类构造函数内”<    }
    Double GetArea()
    {
    Int width=abS
  (rightBottom.x—leftTop.x);
    int height=abs
  (rightBottom.y—leftTop.y);
    Return width*height;
    }
    double GetGirth()
    {
    int width=abS
  (rightB0ttom.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<<“myCi rcle:”<<“Area=”
<GetArea()<<“\t”
    <<“Girth=”<
GetGirth()<  ∥********4********
    cout<<”myRectangle:”<<”Are a=“<GetArea()<<“\t,,
    <<“Girth=”<
GetGirth()<}

选项

答案(1)添加语句:virtual double GetArea()=0; (2)添加语句:virtual double GetGirth()=0; (3)添加语句:myshape=mycircle; (4)添加语句:myshape=myRectanqle;

解析 类CMyPoint含有成员变量x和y,抽象基类CMyShape含有两个虚函数GetArea0和GetGirth0,分别用来求图形的面积和周长。类CMyRectangle和类CMyCircle派生于类CMyShape,并都实现了抽象父类的纯虚函数,通过这两个函数可分别求得矩形的面积和周长以及圆的面积和周长。
转载请注明原文地址:https://kaotiyun.com/show/pUAp777K
0

最新回复(0)