应用题   请使用VC6或使用[答题]菜单打开考生文件夹proj2下的工程proj2,其中包含抽象类Shape的声明,以及在此基础上派生出的类Rectangle和Circle的声明,二者都有计算对象面积的函数GetArea()和计算对象周长的函数GetPerim()。程序中位于每个“//****found****”之后的一行语句有错误,请加以改正。改正后程序的输出结果应该是:
    The area of the Circle is 78.5
    The perimeter of the Circle is 31.4
    The area of the Rectangle is 24
    The perimeter of the Rectangle is 20
    注意:只在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“//****found****”。
    #include <iostream>
    using namespace std;
    class Shape
    {
    public:
    Shape() {}
    ~Shape() {}
    //********** found**********
    ______float GetArea () =0;
    //********** found**********
    ______float GetPerim () =0;
    };
    class Circle : public Shape
    {
    public:
    Circle (float radius): itsRadius (radius) {}
    ~Circle() {}
    float GetArea () { return 3.14 * itsRadius * itsRadius; }
    float GetPerim ()
    {return 6.28 * itsRadius;}
    private:
    float itsRadius;
    };
    class Rectangle : public Shape
    {
    public:
    //********** found**********
    Rectangle(float len, float width):
    ______{};
    ~Rectangle(){};
    virtual float GetArea()
    { return itsLength * itsWidth; }
    float GetPerim ()
    { return 2 * itsLength + 2 * itsWidth;}
    virtual float GetLength() { return itsLength; }
    virtual float GetWidth () { return itsWidth; }
    private:
    float itsWidth;
    float itsLength;
    };
    int main()
    {
    //********** found**********
    ______
    sp = new Circle(5);
    cout << 'The area of the Circle is' << sp->GetArea () << endl;
    cout << 'The perimeter of the Circle is' << sp->GetPerim () << endl;
    delete sp;
    sp = new Rectangle(4, 6);
    cout << 'The area of the Rectangleis' << sp->GetArea() << endl;
    cout << 'The perimeter of the Rectangle is' << sp -> GetPerim () <<endl;
    delete sp;
    return 0;
    }
 
【正确答案】virtual virtual itsLength(len), itsWidth(width) Shape*sp;
【答案解析】[考点] 本题考查抽象基类Shape类及其派生类Circle和Rectangle,其中涉及纯虚函数、构造函数、析构函数和成员函数。 (1)和(2)主要考查考生对纯虚函数定义的掌握,纯虚函数前要添加关键字virtual。 (3)主要考查考生对构造函数的掌握,使用成员列表初始化。 (4)主要考查考生对指针的掌握,由下一条语句:sp=new Circle(5);,可知sp为Shape型指针。