问答题

使用VC++6.0打开考生文件夹下的源程序文件3.cpp,要求编写一个CMyShape类,含有求面积、周长等的纯虚函数,然后编写一个CRectangle类和CCircle类继承CMyShape,并实现求面积、周长的两个函数。在main函数中测试得到下面的结果:   在CMyShape类构造函数内   在CCircle类构造函数内   在CMyShape类构造函数内   在CReetangle类构造函数内   myCircle:Area=314.159Girth=62.8319   myRectangle:Area=900Girth=120   具体要求如下:   (1)定义求面积的纯虚函数,请在注释1后添加适当的语句。   (2)定义求周长的纯虚函数,请在注释2后添加适当的语句。   (3)请在注释3和注释4后添加适当的语句。   注意:除在指定位置添加语句之外,不要改动程序中的其他内容。   试题程序:   #include   #inelude      #definePI3.1415926   classCMyPoint   {   public:   intX,y;   CMyPoint(inttx,intty):x(tx),y(ty){}   };   classCMyShape   {   public:   CMyShape(){cout{{"在CMyShape类构造函数内"   <  //********1********   //********2********   protected:   };   classCCircle:publicCMyShape   {   public:   CCircle(CMyPointi,doublej):CMyShape(),arc-   Center(i),radius(j)   {   cout<<"在CCircle类构造函数内"<  }   doubleGetArea()   {   returnPI*radius*radius:   }   doubleGetGirth()   {   return2*PI*radius:   }   private:   CMyPointarcCenter;   doubleradius;   };   classCRectangle:publicCMyShape   {   public:   CRectangle(CMyPointIt,CMyPomtrb):leftTop(it),   rightBottom(rb),CMyShape()   {   cout<<"在CRectangle类构造函数内"<  }   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="<

【参考答案】

(1)应添加“virtualdoubleGetArea()=0;”。   (2)应添加“virtualdoubleGet......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)