刷题刷出新高度,偷偷领先!偷偷领先!偷偷领先! 关注我们,悄悄成为最优秀的自己!

简答题

阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。
[说明]
设计RGB方式表示颜色的调色板,进行绘图,其类图如下图所示。该程序的C++代码附后。

类图

[C++代码]

    #include<iostream>

    #include <stdlib. h>

    #include <crime>

    using namespace
std;

    class MyColor {

    private:

    int red;  
   int green;      int blue;

    public:

    MyColor(){red = 0;
green = 0; blue = 0;  }

    ~MyColor () { }

    MyColor(int red,
 int green,  int blue)  {

    this->red = red; this->green = green; this->blue =
blue;}

    void print()
 {

    cout<<"Red: " << red << "\tGreen: " << green << "\tBlue " << blue

    << endl;

    }

    };

    class Palette {

    private:

    int number;
MyColor** palette;

    public:

    Palette(){ number
= 256; palette = (MyColor**)malloc

    (sizeof (MyColor*)
*number);  }

    ~Palette () {

    for (int i = 0; i < number; i++)  { delete palette[i];  }

    ______;

    }

    Palette(MyColor**
pale,  int number)  {

    ______ = number;

    palette =
(MyColor**)malloc(sizeof(MyColor*)*number);

    memcpy(palette,
pale,sizeof(pale)*number);

    }

    //其他方法略

    void print()
 {

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

    cout << i << ":";

    palette [i] ->print ();

    }

    }

    };

    class Drawing{

    public:

    ______ int
COLORNUMBER = 16;

    public:

    ~Drawing()  {  }

    void draw()
 {

    Palette* palette;

    int red,
 green,  blue;

    MyColor*
color[COLORNUMBER];

    srand ((unsigned)
time (0));

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

    red=rand()  %
256; green = rand()  % 256; blue = rand() % 256;

    color[i] = ______
(red,  green, blue);

    }

    palette = new
Palette (color,  COLORNUMBER);

    palette->print ();

    for  (int
i=0; i <
COLORNUMBER; i++)

    delete color[i];

    }

    };

    int main  ()
{

    Drawing * d =
______;

    d->draw ();

    delete d;

    }

使用微信搜索喵呜刷题,轻松应对考试!

答案:

free(palette)
this->number
static const
new MyColor
new Drawing()

解析:

这道题目是关于类与对象的定义与操作的题目。根据题目给出的说明和代码,我们需要完成一些空缺的填写。以下是详细的解析:

首先,关于Palette类的析构函数中的内存释放问题。在析构函数中,我们需要释放使用malloc函数动态分配的内存空间。因此,第一个空应该填写释放动态内存的函数,即free(palette)。

其次,关于Palette类的构造函数中的属性赋值问题。在构造函数中,我们需要设置当前对象的number属性,所以第二个空应该填写this->number。

接着,关于Drawing类中COLORNUMBER的定义问题。为了保证COLORNUMBER在程序运行期间不发生改变,我们需要将其定义为静态常量,所以第三个空应该填写static const。

然后,关于Drawing类的draw()方法中创建MyColor对象的问题。在这个方法中,我们需要创建MyColor对象,所以第四个空应该填写调用MyColor的构造方法创建MyColor对象的语句,即new MyColor(red, green, blue)。

最后,关于main()函数中创建Drawing对象的问题。在main函数中,我们需要创建一个Drawing类的对象指针d,所以第五个空应该填写创建Drawing类对象的构造函数,即new Drawing()。

创作类型:
原创

本文链接:阅读下列说明和C++代码,填补代码中的空缺,将解答填入答题纸的对应栏内。[说明]设计RGB方式表示颜

版权声明:本站点所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明文章出处。

让学习像火箭一样快速,微信扫码,获取考试解析、体验刷题服务,开启你的学习加速器!

分享考题
share