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

 

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

�返回
 

温馨提示:

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

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

在第5单元作业【4】以及本单元作业【1】的代码基础上,实现类间的继承关系

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

1
以MyShape作为基类,修改MyRectangle类和MyCircle类从MyShape派生(10分)

题目难度:中

 

题目内容:

基于本单元作业【1】的代码,以及第5单元作业【4】的MyRectangle类和MyCircle类进行修改

修改MyShape类

  1. 添加一个成员showShape(),将Draw()函数体中的所有代码都移动到showShape()函数中

  2. 修改Draw()函数的声明,使之成为纯虚函数

  3. 依据下面MyRectangle类以及MyCircle类的需要,修改MyShape的构造函数

修改MyRectangle类:

  1. 以公有方式继承 MyShape 类

  2. 将所有已经移动到MyShape类中的数据成员和函数成员都删除

  3. 修改所有的构造函数
    1)使之能够将基类中的私有成员type_的值设置为字符串“myrectangle”
    2)保留对矩形坐标初始化的代码
    3)必要的话,通过基类构造函数和基类成员函数对已经移动到基类中的数据成员进行初始化
    4)删除构造函数中的所有输出语句

  4. 修改MyRectangle类的Draw()函数,使之仅仅做如下动作:
    1)首先调用基类成员函数 showShape()
    2)然后输出矩形左上角顶点以及矩形宽和高并换行。此处保留原有的代码即可,输出格式同第五单元作业【4】

  5. 删除showScreen()函数

  6. 如有必要,则增加其他数据成员及函数成员

  7. 不要输出任何未要求输出的信息,不然会导致系统扣分。

修改MyCircle类:

  1. 以公有方式继承 MyShape 类

  2. 将所有已经移动到MyShape类中的数据成员和函数成员都删除

  3. 修改所有的构造函数
    1)使之能够将基类中的私有成员type_的值设置为字符串“mycircle”
    2)保留对圆心坐标及半径初始化的代码
    3)必要的话,通过基类构造函数和基类成员函数对已经移动到基类中的数据成员进行初始化
    4)删除构造函数中的所有输出语句

  4. 修改MyCircle类的Draw()函数,使之仅仅做如下动作:
    1)首先调用基类成员函数 showShape()
    2)然后输出圆心坐标、半径并换行。此处保留原有的代码即可,输出格式同第五单元作业【4】,保持不变

  5. 删除showScreen()函数

  6. 如有必要,则增加其他数据成员及函数成员

  7. 不要输出任何未要求输出的信息,不然会导致系统扣分。

main() 函数:

需使用如下main()函数及辅助类Helper(不得更改)

  1. template <typename T>
  2. struct Helper
  3. {
  4.     typedef char SmallType;
  5.     typedef int LargeType;
  6.  
  7.     template <typename U>
  8.     static char Test(U(*)[1]);
  9.     template <typename U>
  10.     static int Test(...);
  11.  
  12.     const static bool Result = sizeof(Test<T>(NULL)) == sizeof(LargeType);
  13. };
  14.  
  15. int main() {
  16.   int width, height;
  17.   cin >> width >> height;
  18.    
  19.   Screen *screen = Screen::getInstance(width, height);
  20.  
  21.   if (!Helper<MyShape>::Result) cout << endl;
  22.   MyShape *s1, *s2;
  23.  
  24.   s1 = new MyRectangle();
  25.   s1->setScreen(*screen);
  26.   s1->setColor(0, 0, 0xff);
  27.    
  28.   s2 = new MyCircle(100,110,50, screen);
  29.    
  30.   s1->Draw();
  31.   s2->Draw();
  32.  
  33.   delete s1, s2;  
  34.   screen->deleteInstance();
  35.    
  36. #ifdef DEBUG
  37.   std::cin.get();
  38. #endif
  39.   return 0;
  40. }

输入格式:

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

输出格式:

字符串

输入样例:

600 400

输出样例:

enter screen

[600X400]myrectangle(0,0,255)

10 10 90 90

[600X400]mycircle(255,255,255)

100 110 50

leave screen

注意:输出样例共有7行,最后一行为空行

EGE绘图练习补充说明

当你做完本次作业,这一季的课程就要结束了,即将迎来期末考试。

不过,我希望你能继续将这次在线编程的代码在课后增加EGE的绘图函数,让这些图形真正在屏幕上绘制出来。

你一定会遇到各种困难。但是,成功只属于那些能够克服困难的人。

时间限制: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(std::string string1);
	MyShape(std::string string1, 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 换行。
	virtual void Draw() = 0;//修改MyShape类2.修改Draw()函数的声明,使之成为纯虚函数
	void showShape();//修改MyShape类1.添加一个成员showShape(),将Draw()函数体中的所有代码都移动到showShape()函数中
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(std::string string1) : R_(255), G_(255), B_(255), type_(string1)
{

}

MyShape::MyShape(std::string string1, Screen *screen) : R_(255), G_(255), B_(255), type_(string1)
{
	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::showShape()//修改MyShape类1.添加一个成员showShape(),将Draw()函数体中的所有代码都移动到showShape()函数中
{
	std::cout << "[" << screen_->getWidth() << "X" << screen_->getHeight() << "]" << type_ << "(" << R_ << "," << G_ << "," << B_ << ")" << std::endl;
}

class MyRectangle :public MyShape {//修改MyRectangle类:1.以公有方式继承 MyShape 类
private:
	int x1_, y1_, x2_, y2_;

	int getWidth() {
		return x2_ - x1_;
	}

	int getHeight() {
		return y2_ - y1_;
	}

public:
	MyRectangle() :MyShape("myrectangle") {
		//修改MyRectangle类:3.修改所有的构造函数
		//	1)使之能够将基类中的私有成员type_的值设置为字符串“myrectangle”
		//	2)保留对矩形坐标初始化的代码
		//	3)必要的话,通过基类构造函数和基类成员函数对已经移动到基类中的数据成员进行初始化
		//	4)删除构造函数中的所有输出语句
		x1_ = y1_ = 10;
		x2_ = y2_ = 100;
	}

	void setCoordinations(int x1, int y1, int x2, int y2) {
		x1_ = x1;
		y1_ = y1;
		x2_ = x2;
		y2_ = y2;
	}

	void Draw() {
		//修改MyRectangle类:4.修改MyRectangle类的Draw()函数,使之仅仅做如下动作:
		//	1)首先调用基类成员函数 showShape()
		//	2)然后输出矩形左上角顶点以及矩形宽和高并换行。此处保留原有的代码即可,输出格式同第五单元作业【4】
		showShape();
		cout << x1_ << " " << y1_ << " " <<
			this->getWidth() << " " <<
			this->getHeight() << endl;
	}
};

class MyCircle :public MyShape {//修改MyCircle类:1.以公有方式继承 MyShape 类
private:
	int x_, y_, radius_;

public:
	MyCircle(int x, int y, int radius, Screen* screen) :MyShape("mycircle", screen) {
		//修改MyCircle类:3.修改所有的构造函数
		//	1)使之能够将基类中的私有成员type_的值设置为字符串“mycircle”
		//	2)保留对圆心坐标及半径初始化的代码
		//	3)必要的话,通过基类构造函数和基类成员函数对已经移动到基类中的数据成员进行初始化
		//	4)删除构造函数中的所有输出语句
		x_ = x;
		y_ = y;
		radius_ = radius;
	}

	void setCenter(int x, int y) {
		x_ = x;
		y_ = y;
	}

	void setRadius(int radius) {
		radius_ = radius;
	}

	void Draw() {
		//修改MyCircle类:4.修改MyCircle类的Draw()函数,使之仅仅做如下动作:
		//	1)首先调用基类成员函数 showShape()
		//	2)然后输出圆心坐标、半径并换行。此处保留原有的代码即可,输出格式同第五单元作业【4】,保持不变
		showShape();
		cout << x_ << " " << y_ << " " << radius_ << endl;
	}
};

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

template <typename T>
struct Helper
{
	typedef char SmallType;
	typedef int LargeType;

	template <typename U>
	static char Test(U(*)[1]);
	template <typename U>
	static int Test(...);

	const static bool Result = sizeof(Test<T>(NULL)) == sizeof(LargeType);
};

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

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

	if (!Helper<MyShape>::Result) cout << endl;
	MyShape *s1, *s2;

	s1 = new MyRectangle();
	s1->setScreen(*screen);
	s1->setColor(0, 0, 0xff);

	s2 = new MyCircle(100, 110, 50, screen);

	s1->Draw();
	s2->Draw();

	delete s1, s2;
	screen->deleteInstance();

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