找考题网-背景图
问答题

【说明】
以下C++程序的功能是计算三角形、矩形和正方形的面积并输出。程序由4个类组成:类 Triangle、Rectangle和Square分别表示三角形、矩形和正方形:抽象类Figure提供了一个纯虚函数getAxea(),作为计算上述3种图形面积的通用接口。
【C++代码】
#include<iostream>
#include<cmath>
using namespace std;
class Figure
public:
virtual double getArea()=0;//纯虚函数
;
class Rectangle : (1)
protected:
double height;
double width;
public:
Rectangle()
Rectangle(double height, double width)
this->height=height;
this->width=width;

double getArea()
return (2) ;

;
class Square: (3)
public:
Square(double width)
  (4) ;

;
class Triangle: (5)
private:
double la,lb,lc;
public:
Triangle(double la,double lb,double lc)
this->la=la;this->1b=1b;this->lc=lc;

double getArea()
double s=(la+lb+lc)/2.0;
return sqrt(s*(s-l
  • a)*(s-l
  • b)*(s-l
  • c));

    int main() Figure *figures[3]=new Triangle(2,3,3),new Rectangle(5,8), new Square(5);
    for(int i=0;i<3;i++)
    cout<<"figures["<<i<<"]area="<<(figures[i])->getArea()<<endl;

    return 0;

【参考答案】

(1) public Figure
(2) height *width
(3) public Rectangle
(4) height=this->width=width
(5) public Figure