2.按以下要求编写程序 (1) 创建一个Rectangle类,添加width和height两个成员变量 (2) 在Rectangle中添加两种方法分别计算矩形的周长和面积 (3) 编程利用Rectangle输出一个矩形的周长和面积

题目

2.按以下要求编写程序 (1) 创建一个Rectangle类,添加width和height两个成员变量 (2) 在Rectangle中添加两种方法分别计算矩形的周长和面积 (3) 编程利用Rectangle输出一个矩形的周长和面积


相似考题
更多“2.按以下要求编写程序 (1) 创建一个Rectangle类,添加width和height两个成员变量 (2) 在Rectangle中添加两种方法分别计算矩形的周长和面积 (3) 编程利用Rectangle输出一个矩形的周长和面积”相关问题
  • 第1题:

    使用RECTANGLE命令绘制一个矩形,并使该矩形具有厚度为100。正确的方法为()。

    A.用RECTANGLE命令先绘一个矩形,然后用ELEV命令设置厚度为100

    B.用ELEV命令设置厚度为100,然后用RECTANGLE命令绘制矩形

    C.用RECTANGLE命令先绘一个矩形,然后用CHANGE命令改变厚度为100

    D.在RECTANGLE命令中先设置其厚度为100,然后再绘制矩形

    E.用RECTANGLE命令先绘一个矩形,然后再用RECTANGLE命令设置厚度为100


    本题答案:C, D

  • 第2题:

    阅读以下说明和C++代码,将应填入(n)处的字句写在对应栏内。

    【说明】

    以下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-la)*(s-lb)*(s-lc));

    }

    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
    (1) public Figure (2) height *width (3) public Rectangle (4) height=this->width=width (5) public Figure 解析:根据题述“抽象类Figure提供了一个纯虚函数getArea(),作为计算上述3种图形面积的通用接口”,可知类Rectangle均应继承自类Figure,并实现其抽象方法getArea。故空(1)应填 public Figuare。
    getArea方法是计算面积的,空(2)是返回该类的代表的图形面积。类Rectangle代表矩形,而矩形的面积的计算公式是长与宽的乘积,类Rectangle的成员变量height和width分别代表了长和宽,故空(2)应填height*width。
    类Square按理应该继承自Figuare,但并未实现其抽象方法getArea,若继承自Figure则不能被实例化,而题中main函数中已将其实例化,不符合题意,因此不能继承自Figure。考虑到正方形其实就是长和宽相等的矩形,因此Square可从Rectangle继承,故空(3)应填public Rectangle。这样,计算面积仍用Rectangle的getArea方法,这就要求将height和width正确赋值,题中已将这两个成员变量声明为protected,因此这两个变量继承为子类的变量,故空(4)应填height=this->width=width。
    空(5)同空(1),应填public Figure。

  • 第3题:

    下面程序段中,说法正确的是( ) 。class location{ public : int GetX(); }; class rectangle:private location { public: int GetW(); };

    A.类rectangle的私有派生类是location

    B.类rectangle中的public成员在类location中是私有的

    C.类location中的public成员在类rectangle中是不可访问的

    D.viodf(){Rectangler;intX=r.GetX();}其中intX=r.GetX();是非法语句


    正确答案:D

  • 第4题:

    阅读以下说明和C++程序,将应填入(n)处的语句写在的对应栏内。

    【说明】

    以下程序的功能是计算三角形、矩形和正方形的面积并输出。

    程序由4个类组成:类Triangle、Rectangle和Square分别表示三角形、矩形和正方形;抽象类Figure提供了一个纯虚拟函数getArea(),作为计算上述3种图形面积的通用接口。

    【C++程序】

    include<iostream.h>

    include<math.h>

    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){

    double la;

    double lb;

    double lc;

    Public:

    Triangle(double la, double lb, double lc) {

    This->la=la; this->lb=lb; this->lc=lc;

    }

    double getArea() {

    double s = (la+lb+±c)/2.0;

    return sqrt(s,(s-la)*(s-lb)*(s-Ic));

    }

    };

    void 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;

    }

    }


    正确答案:(1)public Figure (2) height * width (3) public Rectangle (4)this->height=this->width=width (5)public Figure
    (1)public Figure (2) height * width (3) public Rectangle (4)this->height=this->width=width (5)public Figure 解析:类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,它们都需要求各自的面积,而抽象类Figure提供了一个纯虚拟函数getArea(),所以类Rectangle以类Figure为基类,公有继承,所以(1)空应填入“public Figure”。(2)空处为类Rectangle中求面积函数的返回值,所以根据长方形面积公式应填入“height*width”。
    类Square表示正方形,它是特殊的长方形,所以可以从代表长方形的类Rectangle公有继承它的成员,所以(3)空应填入“public Rectangle”。(4)空处为类Square的构造函数的函数休,显然是长等于宽,所以应填入"this->height=this->width=width”。
    最后类Triangle同样从虚基类公有继承,所以(5)空应填入“public Figure”。

  • 第5题:

    阅读以下说明和Java源程序,将应填入(n)处的字句写在答题纸的对应栏内。

    说明

    以下程序的功能是计算三角形、矩形和正方形的面积并输出。

    程序由5个类组成:AreaTest是主类,类Triangle、Rectangle和Square分别表示三角形、矩形和正方形,抽象类Figure提供了一个计算面积的抽象方法。

    程序

    public class AreaTest{

    public static void main(String args[]){

    Figure[]figures={

    new Triangle(2,3,3),new Rectangle(5,8), new Square(5)

    };

    for(int i=0;i<figures.1ength;i++){

    System.out.println(figures[i]+"area="+figures[i].getArea());

    }

    }

    }

    public abstract class Figure{

    public abstract double SetAJea();

    public class Rectangle extends (1) {

    double height;

    double width;

    public Rectangle(double height,double width){

    this.height=height;

    this.width=width;

    }

    public String toString(){

    return "Rectangle:height="+height+",width="+width+":";

    }

    public double getArea() { return (2);

    } } public class Square extends (3) {

    public Square(double width) {

    (4);

    }

    public String toString() {

    return "Square:width="+width+":";

    } } public class Triangle extends (5). {

    double la;

    double lb;

    double lc;

    public Triangle(double la,double lb,double lc) {

    this.la=la; this.lb=lb; this.lc=lc;

    public String toString(){

    return "Triangle: sides="+la+","+lb+","+lc+":";

    public double getArea() {

    double s=(la+lb+lc)/2.0;

    return Math.sqrt(s*(s-la)*(s-lb)*(s?1c));

    }

    }


    正确答案:(1)Figure (2)height*width (3)Rectangle (4)super(widthwidth) (5)FiguTe
    (1)Figure (2)height*width (3)Rectangle (4)super(width,width) (5)FiguTe 解析:本题以Java语言为载体,考查面向对象程序设计中的三个重要机制——继承、抽象类和动态绑定的应用。本题所解决的问题与试题6一样,有关的详细描述可参见试题6的分析。
    在Java语言中通过在类中定义抽象方法来创建一个抽象类,或者直接将一个类声明为抽象类。
    由于继承关系已经确定,所以第(1)、(3)、(5)空已经确定,分别为Figure、Rectangle和Figure。第(2)空应填height*widthn
    Java中有一个特殊的函数super,用于在派生类中调用其基类的构造函数。利用 super,我们可以借助Rectangle的带有2个参数的构造函数将正方形的边长width传递到Rectangle的height和width中,所以第(4)空应填super(width,width)。

  • 第6题:

    阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内

    [说明]

    以下程序的功能是计算三角形、矩形和正方形的面积并输出。

    程序由4个类组成:类Triangle,Rectangle和Square分别表示三角形、矩形和正方形;抽象类Figure提供了一个纯虚拟函数getArea(),作为计算上述三种图形面积的通用接口。

    [C++程序]

    include<iostream.h>

    include<math.h>

    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) {

    double la;

    double lb;

    double lc;

    public:

    triangle(double la,double lb,double lc){

    this->la=la;thiS->ib;this->lc;

    }

    double getArea(){

    double s=(la+lb+lc)/2.0;

    return sqrt(s*(s-la)**(s-lb)*(s-lc));

    }

    };

    viod 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)->getarea()<<endl;

    }

    }

    };


    正确答案:(1) public Figure (2) height*width (3) public Rectangle (4) this->height=this->width=width (5) public Figure
    (1) public Figure (2) height*width (3) public Rectangle (4) this->height=this->width=width (5) public Figure 解析:本题考查C++编程中的几个重要概念。
    Figure类是一个抽象类,其他三个类rectangle、square、triangle都要直接或间接继承该类,所以(1)(5)处应为“public Figure”。(2)处是要计算矩形面积,矩形面积等于长乘以宽,所以(2)处应为“height* ]width”。正方形是一个特殊的矩形,所以可以继承矩形类,所以(3)处应为“publicRectangle”,(4)处应为“this->height=this->width=width”。

  • 第7题:

    用RECTANGLE命令画成的一个矩形,它包含的图元的个数是几个()。

    • A、1
    • B、2
    • C、不确定
    • D、4

    正确答案:D

  • 第8题:

    在AutoCAD中应用面积命令“area”有以下几种查询方式()。

    • A、查询选定对象的面积和周长
    • B、查询按顺序指定一系列点构成多边形的面积和周长
    • C、添加新区域
    • D、减去新区域

    正确答案:A,B,C,D

  • 第9题:

    Which statements concerning the effect of the statement gfx.drawRect(5, 5, 10, 10) are true, given that gfx is a reference to a valid Graphics object?()  

    • A、The rectangle drawn will have a total width of 5 pixels.
    • B、The rectangle drawn will have a total height of 6 pixels.
    • C、The rectangle drawn will have a total width of 10 pixels.
    • D、The rectangle drawn will have a total height of 11 pixels.

    正确答案:D

  • 第10题:

    单选题
    The length of a rectangle is 5 more than the side of a square, and the width of the rectangle is 5 less than the side of the square. If the area of the square is 45, what is the area of the rectangle?
    A

    20

    B

    25

    C

    45

    D

    50

    E

    70


    正确答案: D
    解析:
    设正方形的边长为x,那么根据题意有x2=45,所以矩形的面积为 (x+5)(x-5) =x2-25=45-25=20。

  • 第11题:

    判断题
    使用RECTANGLE命令创建的矩形,其边将总是水平或竖直的。()
    A

    B


    正确答案:
    解析: 暂无解析

  • 第12题:

    填空题
    在NX中,选择区域的类型有矩形(Rectangle)和()。

    正确答案: 套索(Lasso)
    解析: 暂无解析

  • 第13题:

    请编写一个完整的Java Application程序,能够计算圆的周长和面积。

    要求:

    (1)定义点类CPoint;

    (2)定义圆类CCircle继承自类CPoint,类中属性包括:圆心,半径,类中方法包括:求周长perimeter()、求面积area();

    (3) 定义主类CCircleDemo,利用类CCircle输出一个圆的圆心,半径,周长和面积


    答案:如下

    解析:

    public class CPoint{

     private float radius;

     public CCircle(){

      this.radius=5.0f;

     }

     public CCircleDemo(float radius){

      this.radius=radius;

     }

     public void Area(){

      System.out.println("半径为"+this.radius+"面积是:"+(3.14*radius*radius));

     }

     public void perimeter(){

      System.out.println("半径为"+this.radius+"周长是:"+(3.14*2*radius));

    }


    public static void main(String arg[]){

     CCircle c1=new CCircle();

     c1.Area();

     c1.perimeter();

      CCircle c2=new CCirclet(9.0f);

     c2.Area();

     c2.perimeter();

    }

    }


  • 第14题:

    使用VC6打开考生文件夹下的工程test41_3。此工程包含一个test41_3.cpp,其中定义了类Rectangle,但该类的定义并不完整。请按要求完成下列操作,将程序补充完整。

    (1)定义类Rectangle的私有数据成员left,top和fight,bottom,它们都是int型的数据。请在注释“//**1**”之后添加适当的语句。

    (2)添加类Rectangle的带四个int型参数1、t、r、b的构造函数的声明,并使这四个参数的默认值均为0,请在注释“//**2**”之后添加适当的语句。

    (3)添加类Rectangle的成员函数SetTop()参数为int型的t,作用为把t的值赋给类的数据成员top,添加类Rectangle的成员函数SetBottom()参数为int型的t,作用为把t的值赋给类的数据成员bottom,请在注释“//**3**”之后添加适当的语句。

    (4)完成派生类Rectangle的成员函数Show()的定义,使其以格式“right-bottom point is(right,bottom)”输出,请在注释“//**4**”之后添加适当的语句。

    源程序文件test41_3.cpp清单如下:

    include <iostream.h>

    class Rectangle

    {

    // ** 1 **

    int right, bottom;

    public:

    // ** 2 **

    ~ Rectangle(){};

    void Assign(int 1, int t, int r, int b);

    void SetLeft(int t){left = t;}

    void SetRight(int t){right = t;}

    // ** 3 **

    void SetBottom(int t){bottom = t;}

    void Show();

    };

    Rectangle::Rectangle(int 1, int t, int r, int b)

    {

    left = 1; top = t;

    right = r; bottom = b;

    }

    void Rectangle::Assign(int 1, int t, int r, int b)

    {

    left = 1; top = t;

    right = r; bottom = b;

    }

    void Rectangle::Show()

    {

    cout<<"left-top point is ("<<left<<","<<top<<")"<<'\n';

    // ** 4 **

    }

    void main()

    {

    Rectangle rect;

    rect.Show();

    rect.Assign(100,200,300,400);

    rect.Show();

    }


    正确答案:(1) int left top; (2) Rectangle(int 1=0 int t=0 int r=0 int b=0); (3) void SetTop(int t){top=t;} (4) cout"right-bottom point is ("right""bottom")"'\n';
    (1) int left, top; (2) Rectangle(int 1=0, int t=0, int r=0, int b=0); (3) void SetTop(int t){top=t;} (4) cout"right-bottom point is ("right","bottom")"'\n'; 解析:主要考查考生对于类的定义和定义一般成员函数的掌握,其中(2)中为了使构造函数可以不带参数,使用了对于参数给定默认值的方法,这点需要考生注意,(4)中连续的字符流的输出可以连续使用“”符号实现。

  • 第15题:

    阅读以下说明和C++代码,填入(n)处。

    [说明]

    以下C++代码使用虚函数实现了同一基类shape派生出来的Class rectangle、Class triangle、Class circle实现了计算矩形、圆形面积的计算。仔细阅读以下代码,将(n)处语句补充完整。

    [代码5-1]

    include<iostream.h>

    define PI 3.14159

    class shape {//基类

    protected:

    (1);

    public:

    (2);

    (3);

    };

    [代码5-2]

    class rectangle: public shape {

    public:

    rectangle (int x2,int y2,int r2): (4) {};

    double area ( ) {return x*y; };

    };

    class circle: public shape {

    public:

    circle (int x3,int y3,int r3):(5){};

    double area ( ) {return r*r*PI; };

    };

    [代码5-3]

    void main ( )

    {

    rectangle r (10,20,0);

    circle c (0,0,30);

    shape (6);

    cout<<"长方形面积="<<s1->area ( ) <<endl;

    cout<<"圆形面积="<<s2->area ( ) <<endl;

    }

    [运行结果]

    长方形面积=200

    圆形面积=2827.43


    正确答案:(1)intxyr; (2)shape(int x1int y1int r1): x(x1)y(y1)r(r1){}; (3)double virtual area()=0; (4)shape(x2y2r2) (5)shape(x3y3r3) (6)*s1=&r*s2=&c;
    (1)intx,y,r; (2)shape(int x1,int y1,int r1): x(x1),y(y1),r(r1){}; (3)double virtual area()=0; (4)shape(x2,y2,r2) (5)shape(x3,y3,r3) (6)*s1=&r,*s2=&c; 解析:本题C++代码使用虚函数用同一基类shape派生出来的Class rectangle、Class triangle、Class circle实现了计算矩形、圆形面积的计算。各空实现的功能是:(1)x,y存储长与宽,r存储半径;(2)构造函数;(3)面积虚函数,旨在定义不同面积公式;(4)构造函数;(5)构造函数;(6)类变量定义,根据下文用到的变量可推知。

  • 第16题:

    阅读下列C++程序和程序说明,将应填入(n)处的字句写在对应栏内。

    【说明】

    以下C++程序的功能是计算三角形、矩形和正方形的面积并输出。程序由4个类组成:类Triangle、Rectangle和Square分别表示三角形、矩形和正方形;抽象类Figure提供了一个纯虚拟函数getArea(),作为计算上述3种图形面积的通用接口。

    include<iostream.b>

    include<math.h>

    class Figure{

    public:

    virtual double getArea0=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) {

    double la;

    double lb;

    double lc;

    public:

    Triangle(double la, double lb, double lc){

    this->la=la; this->lb; this->lc;

    }

    double getArea(){

    double s=(la+lb+lc)/2.0;

    return sqrt(s*(s-la)**(s-lb)*(s-lc));

    }

    };

    viod 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;

    }

    }


    正确答案:(1)public Figure (2)height*width或width*height或this->height*this->width或this->width*this->height (3)public Rectangle (4)this->height=this->width=width或height=this->width=width (5)public Figure
    (1)public Figure (2)height*width或width*height或this->height*this->width或this->width*this->height (3)public Rectangle (4)this->height=this->width=width或height=this->width=width (5)public Figure 解析:(1)public Figure
    本处由于Rectangle是派生类,需要公有继承Figure。
    (2)height*width或width*height或this->height*this->width或this->width*this->height
    本处是计算矩形的面积。
    (3)public Rectangle
    本处由于Square是派生类,需要公有继承Rectangle。
    (4)this->height=this->width=width或height=this->width=width
    本处是正方形的构造函数,是给继承的Rectangle赋初值,由于正方形长等于宽,因此应该填入:this->height=this->width=width或height=this->width=width。
    (5)public Figure
    本处由于Triangle是派生类,需要公有继承Figure,因此应该填入:public Figure。

  • 第17题:

    阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。

    说明

    以下程序的功能是计算三角形、矩形和正方形的面积并输出。

    程序由4个类组成:类Triangle、Rectangle和Square分别表示三角形、矩形和正方形;抽象类Figure提供了一个纯虚拟函数getArea(),作为计算上述三种图形面积的通用接口。

    c++程序

    include <iostream.h>

    inclule <math.h>

    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) {

    double la;

    double lb;

    double lc;

    public:

    Triangle(double la, double lb, double lc) {

    this->la = la; this->lb ='lb; this->lc = lc;

    }

    double getArea() {

    double s = (la+lb+lc)/2.0;

    return sqrt(s*(s-la)*(s-lb)*(s-lc));

    } }; void main() {

    Figure* figures[3] = {

    new Triangle(2,3,3), new Rectangle(5,8), new Square(5)};

    for (int i = 0; i < 3; i++) {

    com << "figures[" << i << "] area =" << (figures[i])->getArea0 << endl;

    }


    正确答案:(1)public Figure (2)height*width (3)public Rectangle (4)height=this->width=width (5)public Figure
    (1)public Figure (2)height*width (3)public Rectangle (4)height=this->width=width (5)public Figure 解析:本题以C++语言为载体,考查面向对象程序设计中的三个重要机制——继承、抽象类和动态绑定的应用。
    继承是父类和子类之间共享数据和方法的机制。这是类之间的一种关系,在定义和实现一个类的时候,可以在一个已经存在的类的基础上进行,把这个已经存在的类所定义的内容作为自己的内容,并加入若干新的内容。
    动态绑定是实现动态多态的基础,在C++中,重置是一种动态多态。重置(overriding)的基本思想是,通过一种动态绑定机制的支持,使得子类在继承父类界面定义的前提下,用适合于自己要求的实现去置换父类中的相应实现。
    抽象类的主要作用在于为派生类提供一个基本框架和一个公共的对外接口。在C++语言中,通过定义纯虚函数来定义抽象类。纯虚函数是指只给出函数声明而没有给出实现(包括在类定义的内部和外部)的虚成员函数,其格式为:在函数原型的后面加上符号“=0”。包含纯虚函数的类称为抽象类。一个抽象类的派生类应对其抽象基类的所有纯虚成员函数进行实现。
    本程序的功能是通过一个公共接口getArea()来计算不同形状图形的面积。题目中定义了4个类Figure、Rectangle(长方形)、Square(正方形)和Triangle(三角形)。在完成本题目时,一个重要的环节是确定这4个类之间的继承关系。
    第一步:确定基类。题目的说明已经指出,类Figure是一个抽象类,那么Figure只能作为基类。
    第二步:确定把Figure作为直接基类的派生类。理论上,其余的3个类都可以作为
    Figure的直接派生类。我们先假定Rectangle、Square和Triangle都是Figure的直接派生类,下面结合程序来验证一下这种继承关系是否合理:
    ▲类Rectangle是Figure的派生类,在Rectangle中将getArea进行了重置,所以第(2)空应给出getArea的实现体:计算长方形的面积,即height*width。这时需要考虑的另一个问题是继承的方式。在C++中继承有三种方式:public、private和protected,它用于说明派生类的实例的用户以及派生类的派生类对该派生类的基类成员的访问控制。由于无法确定Rectangle是否会作为其他类的基类(若存在这种情况,该类中的某些成员函数可能会是其他类的公共接口),所以选择public继承方式,则第(1)空应填public Figure。
    ▲类Triangle是Figure的派生类,在该类中也给出了getArea的实现,所以第(5)空应该填public Figure。
    ▲对类Square来说,它的基类可能有两个:Figure和Rectangle。Square本身是一种图形,同时Square也是一种特殊的Rectangle。但是在类Square中没有提供对方法 getArea的重新定义,所以Square不可能作为Figure的派生类。那么,它的基类只能是 Rectangle,所以第(3)空应填publicRectangle。显而易见,第(4)空是要给出计算正方形面积的操作。这时可以借助其基类Rectangle的getArea方法,此时需要做的是将正方形的边长width传递到基类Rectangle的属性height和width中即可,所以第(4)空应填height=this->width=width。这也是第(3)空选择public继承方式的原因。
    [*]
    此时我们可以得出题目中4个类的继承关系如下:
    其中箭头表示继承关系。

  • 第18题:

    使用RECTANGLE命令创建的矩形,其边将总是水平或竖直的。()


    正确答案:正确

  • 第19题:

    使用Rectangle命令创建的矩形,其边总是水平或竖直的。


    正确答案:正确

  • 第20题:

    按住Ctrl键Rectangle可以画()。

    • A、矩形

    正确答案:B

  • 第21题:

    单选题
    关于图3.19的叙述()是不正确的。
    A

    Rectangle类和Circle类都有名为area的属性,这两个属性一定是相同的属性

    B

    Rectangle类和Circle类都有名为getArea的操作,这两个操作一定是相同的操作

    C

    Rectangle中名为length的属性和Circle类中名为radius的属性,这两个属性一定是不同的属性

    D

    Shape类有一个属性,Circle类有两个属性,Rectangle类有三个属性


    正确答案: C
    解析: 在父类与子类的继承关系中,多态这种方法使得在多个子类中可以定义同一个操作或属性名,并在每个子类中可以有不同的实现。Rectangle和Circle都继承于父类Shape,对于Shape而言,会有getArea()的操作。但是子类Rectangle和Circle的getArea方法的实现可以完全不一样的,这就体现了多态的特征。至于选项D://Circle类和Rectangle类都要从Shape类继承一个属性,因此分别有"1+1=2"个属性和"2+1=3"个属性。

  • 第22题:

    问答题
    A rectangle is equal in area to a square with sides of length 12. Is the diagonal of the rectangle greater in length than 20?  (1) The rectangle has a length of 16.  (2) The rectangle has a width of 9.

    正确答案: D
    解析:
    通过计算可知,若长方形长为16或宽为9,面积为144,其对角线长度均小于20,故本题选D项。

  • 第23题:

    单选题
    Which statements concerning the effect of the statement gfx.drawRect(5, 5, 10, 10) are true, given that gfx is a reference to a valid Graphics object?()
    A

    The rectangle drawn will have a total width of 5 pixels.

    B

    The rectangle drawn will have a total height of 6 pixels.

    C

    The rectangle drawn will have a total width of 10 pixels.

    D

    The rectangle drawn will have a total height of 11 pixels.


    正确答案: D
    解析: 暂无解析

  • 第24题:

    单选题
    按住Ctrl键Rectangle可以画()。
    A

    矩形


    正确答案: A
    解析: 暂无解析