网易云课堂_C++程序设计入门(上)_第6单元:丹枫虽老犹多态–继承与多态_第6单元作业【1】-在线编程(难度:易)

 

第6单元作业【1】-在线编程(难度:易)

�返回
 

温馨提示:

1.本次作业属于Online Judge题目,提交后由系统即时判分。

2.学生可以在作业截止时间之前不限次数提交答案,系统将取其中的最高分作为最终成绩。

基于第四单元作业的最终成果,练习如何创建一个基类

依照学术诚信条款,我保证此作业是本人独立完成的。

1
基于第五单元的作业成果,创建一个基类(10分)

题目难度:易

 

题目内容:

本作业基于第5单元作业【4】的代码修改。

 

保留MyScreen类的声明及实现,不做任何改动

 

修改MyRectangle类及MyCircle类

  1. 将MyRectangle类的声明及实现均注释掉,使之不再生效

  2. 将MyCircle类的声明及实现均注释掉,使之不再生效

增加MyShape类:

  1. 颜色处理
    1)将MyRectangle与MyCircle类中表示颜色的数据域成员挪到MyShape类的private区域
    2)将setColor(int R, int G, int B)函数挪到MyShape类中;
    3)将颜色数据成员的getter函数挪到MyShape类中;如果原MyRectangle和MyCircle中没有这些getter函数,则在MyShape类中添加。

  2. 屏幕处理
    1)将MyRectangle与MyCircle类中的Screen*类型的成员挪到MyShape类中
    2)将setScreen函数也挪到MyShape类中

  3. MyShape类构造函数:
    1)根据需要,添加必要的构造函数
    2)所有构造函数均应将颜色初始化为白色,也就是RGB三个颜色分量的值均为255

  4. 添加一个 string 类型的【私有】数据成员 type_
    1)在 MyShape 的构造函数初始化列表中将其初始化为字符串 "myshape"
    2)由你自行决定 type_的访问控制属性,只要它不是public访问属性即可 

  5. 在MyShape类中添加函数 Draw(),在Draw()函数中仅仅实现(不要画蛇添足):
    1) 首先输出屏幕的宽和高,中间以大写的字母“X”连接,并且放置在一对方括号中,形如:[640X480]
    2)  紧随之后输出数据成员type_的值;
    3) 紧随之后再输出使用【半角逗号】分隔的三个颜色。这三个颜色放到一对【半角小括号】中,形如:(255,155,55),其中不包含任何空格
    3) 最后使用 std::endl 换行。

  6. 根据需要,在MyShape类中添加其它函数,例如getter/setter

输入格式:

空格分隔的两个整数,代表屏幕的宽和高

输出格式:

参见样例。不同输入会导致不同的输出信息

输入样例:

560 120

输出样例:

enter screen

[560X120]myshape(255,255,255)

[560X120]myshape(0,0,255)

leave screen

注意:上面的样例输出一共有5行,最后一行为空行

主函数:(不可做任何修改)

  1. int main() {
  2.   int width, height;
  3.   cin >> width >> height;
  4.  
  5.   Screen *screen = Screen::getInstance(width, height);
  6.  
  7.   MyShape shape1(screen);
  8.   MyShape* shape2 = new MyShape();
  9.   shape2->setScreen(*screen);
  10.   shape2->setColor(0, 0, 0xff);
  11.  
  12.   shape1.Draw();
  13.   shape2->Draw();
  14.    
  15.   delete shape2;  
  16.   screen->deleteInstance();
  17.    
  18. #ifdef DEBUG
  19.   std::cin.get();
  20. #endif
  21.   return 0;
  22. }

第五单元作业示例:

以下代码为第五单元作业的参考代码(仅提供了两个图形类的代码)。

建议使用你自己的代码作为本单元作业的基础,尽量不要用本题目所给的代码

  1. class MyRectangle {
  2. private:
  3.     int x1_, y1_, x2_, y2_;
  4.     int R_, G_, B_;
  5.          
  6.     Screen* screen_;
  7.      
  8.     int getWidth() {
  9.         return x2_ - x1_;
  10.     }
  11.      
  12.     int getHeight() {
  13.         return y2_ - y1_;
  14.     }
  15.      
  16. public:
  17.     MyRectangle (int x1, int y1, int x2, int y2, Screen* screen) {
  18.         x1_ = x1;
  19.         y1_ = y1;
  20.         x2_ = x2;
  21.         y2_ = y2;
  22.         R_ = 255; G_ = 255, B_ = 255;
  23.         screen_ = screen;
  24.          
  25.         cout << "myrectangle" << endl;
  26.     
  27.      
  28.     MyRectangle () {
  29.         x1_ = y1_ = 10;
  30.         x2_ = y2_ = 100;
  31.         R_ = 255; G_ = 255, B_ = 255; 
  32.                
  33.         screen_ = 0;
  34.          
  35.         cout << "myrectangle" << endl;
  36.     }
  37.      
  38.     void setCoordinations(int x1, int y1, int x2, int y2) {
  39.         x1_ = x1;
  40.         y1_ = y1;
  41.         x2_ = x2;
  42.         y2_ = y2;
  43.     }
  44.      
  45.     void setScreen(Screen& screen) {
  46.         screen_ = &screen;
  47.     }
  48.      
  49.     void setColor(int R, int G, int B) {
  50.         R_ = R; G_ = G, B_ = B;
  51.     }
  52.      
  53.      
  54.     void Draw () {
  55.         cout << x1_ << " " << y1_ << " " <<
  56.                 this->getWidth() << " " <<
  57.                 this->getHeight() << endl;
  58.         cout << R_ << " " << G_ << " "  << B_ << endl;
  59.     }
  60.      
  61.     void showScreen() {
  62.         cout << screen_->getWidth() << " " << screen_->getHeight() << endl;
  63.     
  64.          
  65. };
  66.  
  67. class MyCircle {
  68. private:
  69.     int x_, y_, radius_;
  70.     int R_, G_, B_;
  71.     Screen* screen_;
  72.      
  73. public:
  74.     MyCircle (int x, int y, int radius, Screen* screen) {
  75.         x_ = x;
  76.         y_ = y;
  77.         radius_ = radius;
  78.         R_ = 255; G_ = 255, B_ = 255;
  79.         screen_ = screen;
  80.          
  81.         cout << "mycircle" << endl;
  82.     
  83.      
  84.     MyCircle () {
  85.         x_ = y_ = 200;
  86.         radius_ = 100;
  87.         R_ = 255; G_ = 255, B_ = 255;       
  88.         screen_ = 0;
  89.          
  90.         cout << "mycircle" << endl;
  91.     }
  92.  
  93.     MyCircle(const MyCircle& aCircle) {
  94.         x_ = aCircle.x_;
  95.         y_ = aCircle.y_;
  96.         radius_ = aCircle.radius_;
  97.         R_ = aCircle.R_;
  98.         G_ = aCircle.G_;
  99.         B_ = aCircle.B_;
  100.         screen_=aCircle.screen_;
  101.         cout << "copy mycircle" << endl;
  102.     
  103.      
  104.     void setCenter(int x, int y) {
  105.         x_ = x;
  106.         y_ = y;
  107.     }
  108.      
  109.     void setRadius(int radius) {
  110.         radius_ = radius;
  111.     }
  112.      
  113.     void setColor(int R, int G, int B) {
  114.         R_ = R; G_ = G, B_ = B;
  115.     }
  116.  
  117.     void setScreen(Screen& screen) {
  118.         screen_ = &screen;
  119.     }
  120.          
  121.     void Draw () {
  122.         cout << x_ << " " << y_ << " " << radius_ << endl;
  123.         cout << R_ << " " << G_ << " "  << B_ << endl;
  124.     }
  125.      
  126.     void showScreen() {
  127.         cout << screen_->getWidth() << " " << screen_->getHeight() << endl;
  128.     
  129.          
  130. };
时间限制:500ms内存限制:32000kb
 
#include <iostream>
#include <string>

using namespace std;

class Screen
{
public:
	int getWidth();
	int getHeight();
public:
	~Screen();//1.为Screen类添加析构函数
	static Screen* getInstance(int width, int height);//3.在Screen类中,增加一个静态公有的 getInstance(int width, int height) 函数,该函数返回instance的值。两个参数均带有默认值,分别为 640 和 480
	void deleteInstance();//2.在Screen类中,添加一个deleteInstance()函数
private:
	int width_;//屏幕的宽
	int height_;//屏幕的高
	std::string enter;//1.在Screen类中,添加两个 string 类型的数据成员 enter 和 leave,并在构造函数中将他们分别初始化为字符串“enter screen”和“leave screen”(每个字符串中只有一个空格分隔两个单词)
	std::string leave;//1.在Screen类中,添加两个 string 类型的数据成员 enter 和 leave,并在构造函数中将他们分别初始化为字符串“enter screen”和“leave screen”(每个字符串中只有一个空格分隔两个单词)
	static Screen* instance;//2.在Screen类中,增加一个 Screen* 类型的静态的私有数据成员 instance;
private:
	Screen(int width, int height);//将Screen类中的所有构造函数都变成 private 成员
	void exitWhenInvalidScreen(int width, int height);//检测屏幕的宽与高是否 符合逻辑
};

int Screen::getWidth()
{
	return width_;
}

int Screen::getHeight()
{
	return height_;
}

Screen::~Screen()//1.为Screen类添加析构函数
{
	std::cout << leave << std::endl;//析构函数应首先输出数据成员leave的内容并换行(使用std::endl)
}

Screen* Screen::getInstance(int width = 640, int height = 480)//3.在Screen类中,增加一个静态公有的 getInstance(int width, int height) 函数,该函数返回instance的值。两个参数均带有默认值,分别为 640 和 480
{
	//4.在getInstance函数中,判断instance的值
	//  1) 若instance值为0(即“instance是一个空指针”)
	//  a.以width和height作为构造函数的参数,使用new运算符创建一个Screen对象
	//  b.将新的Screen对象的地址保存在instance中;
	//  2) 若instance的值不为0(即instance指向一个Screen对象),则返回instance的值
	if (!instance)
	{
		instance = new Screen(width, height);

	}

	return instance;
}

void Screen::deleteInstance()//2.在Screen类中,添加一个deleteInstance()函数
{
	delete instance;//功能:将getInstance()函数中申请的内存归还给操作系统。
	instance = 0;//将数据成员instance设置为空指针
}

Screen::Screen(int width, int height)//将Screen类中的所有构造函数都变成 private 成员
{
	//修改Screen类的构造函数
	//  1) 删除Screen类的默认构造函数,只保留带参构造函数,并修改之使其能初始化数据成员
	//  2) 删除第4单元作业的Screen类的所有构造函数中的【cout << "screen" << endl; 】语句
	//  3) Screen类的所有构造函数【均应输出】数据成员enter中的字符串内容并换行(使用std::endl),但是【不再输出其它信息】
	//  4) Screen类的构造函数仍然使用第四单元作业中所要求的exitWhenInvalidScreen函数检查屏幕宽和高的有效性。(可以直接复用第四单元作业的相关代码);该部分代码必须放在输出数据成员enter的代码之后
	width_ = width;
	height_ = height;
	enter = "enter screen";//1.在Screen类中,添加两个 string 类型的数据成员 enter 和 leave,并在构造函数中将他们分别初始化为字符串“enter screen”和“leave screen”(每个字符串中只有一个空格分隔两个单词)
	leave = "leave screen";//1.在Screen类中,添加两个 string 类型的数据成员 enter 和 leave,并在构造函数中将他们分别初始化为字符串“enter screen”和“leave screen”(每个字符串中只有一个空格分隔两个单词)

	std::cout << enter << std::endl;

	exitWhenInvalidScreen(width, height);//检测屏幕的宽与高是否 符合逻辑
}

void Screen::exitWhenInvalidScreen(int width, int height)//检测屏幕的宽与高是否 符合逻辑
{
	if (width <= 0 || height <= 0)//宽度和高度必须大于0像素(不能等于0像素)
	{
		std::cout << "invalid screen size";//如果宽或者高不满足上述任一条件,则整个程序仅仅输出字符串"invalid screen size",然后退出程序
		exit(0);
	}

	if (width > 1000 || height > 1000)//宽度和高度均不得大于1000像素(可以等于1000像素)
	{
		std::cout << "invalid screen size";
		exit(0);
	}
}

class MyShape
{
public:
	//3.MyShape类构造函数:
	//	1)根据需要,添加必要的构造函数
	//	2)所有构造函数均应将颜色初始化为白色,也就是RGB三个颜色分量的值均为255
	MyShape(Screen *screen);
	MyShape();
	//1.颜色处理
	//	1)将MyRectangle与MyCircle类中表示颜色的数据域成员挪到MyShape类的private区域
	//	2)将setColor(int R, int G, int B)函数挪到MyShape类中;
	//	3)将颜色数据成员的getter函数挪到MyShape类中;如果原MyRectangle和MyCircle中没有这些getter函数,则在MyShape类中添加。
	void setColor(int R, int G, int B);
	//2.屏幕处理
	//	1)将MyRectangle与MyCircle类中的Screen*类型的成员挪到MyShape类中
	//	2)将setScreen函数也挪到MyShape类中
	void setScreen(Screen& screen);
	//5.在MyShape类中添加函数 Draw(),在Draw()函数中仅仅实现(不要画蛇添足):
	//	1) 首先输出屏幕的宽和高,中间以大写的字母“X”连接,并且放置在一对方括号中,形如:[640X480]
	//	2)  紧随之后输出数据成员type_的值;
	//	3) 紧随之后再输出使用【半角逗号】分隔的三个颜色。这三个颜色放到一对【半角小括号】中,形如:(255, 155, 55),其中不包含任何空格
	//	3) 最后使用 std::endl 换行。
	void Draw();
private:
	int R_, G_, B_;
	Screen* screen_;
	//4.添加一个 string 类型的【私有】数据成员 type_
	//	1)在 MyShape 的构造函数初始化列表中将其初始化为字符串 "myshape"
	//	2)由你自行决定 type_的访问控制属性,只要它不是public访问属性即可
	std::string type_;
};

MyShape::MyShape(Screen *screen) :R_(255), G_(255), B_(255), type_("myshape")
{
	screen_ = screen;
}

MyShape::MyShape() :R_(255), G_(255), B_(255), type_("myshape")
{

}

void MyShape::setColor(int R, int G, int B)
{
	R_ = R; G_ = G, B_ = B;
}

void MyShape::setScreen(Screen& screen)
{
	screen_ = &screen;
}

void MyShape::Draw()
{
	std::cout << "[" << screen_->getWidth() << "X" << screen_->getHeight() << "]" << type_ << "(" << R_ << "," << G_ << "," << B_ << ")" << std::endl;
}

Screen* Screen::instance;//8.不要忘记在类外对Screen类的所有静态成员进行初始化,否则编译器会报告链接出错。

int main() {
	int width, height;
	cin >> width >> height;

	Screen *screen = Screen::getInstance(width, height);

	MyShape shape1(screen);
	MyShape* shape2 = new MyShape();
	shape2->setScreen(*screen);
	shape2->setColor(0, 0, 0xff);

	shape1.Draw();
	shape2->Draw();

	delete shape2;
	screen->deleteInstance();

#ifdef DEBUG
	std::cin.get();
#endif
	return 0;
}
原文地址:https://www.cnblogs.com/denggelin/p/5926746.html