创建两个类,并在main函数中创建类的实例。
1. 创建Screen类
2. 创建MyRectangle类
3. 在main()中创建上述类的实例,并调用对象的函数
题目内容:
Screen类:①Screen类的构造函数能够接收屏幕的宽和高两个整型参数(以像素为单位)并存储在类的数据域中
②如果宽与高超过1000,或者其它不合理的设置则用cout输出“invalid screen size”,然后结束程序。(可使用 exit() 函数,该函数在头文件 cstdlib 中)
③Screen类的默认构造函数将屏幕宽和高分别设置为640和480
④Screen类的所有构造函数均应输出字符串“screen”并换行
⑤如有必要,则增加其他数据成员及函数成员,例如数据域成员的getter与setter函数
MyRectangle类:
①MyRectangle类中的数据域有一个唯一与Screen类有关的成员,其类型为 Screen* 类型
②MyRectangle类的构造函数1接受5个参数,其中前4个是整型参数
③按照顺序,整型参数分别为矩形的左上顶点的x、y坐标,以及右下顶点的x、y坐标。(此处不做坐标有效性检查)
④按照顺序,第5个参数为Screen类的对象指针
⑤MyRectangle类的默认构造函数将对角线两个点的坐标均设置为原点坐标(0,0)
⑥MyRectangle类的所有构造函数均应使用cout输出字符串“myrectangle”并换行
⑦MyRectangle类中应提供setCoordinations()用于设置对角线的左侧及右侧顶点坐标;函数的参数的含义及类型和构造函数1的前4个参数相同。
⑧MyRectangle类中应提供setScreen(const Screen&)用于设置该类的实例所对应的Screen对象;
⑨MyRectangle类的Draw()函数应检查坐标的有效性,确保矩形的顶点坐标是合理的、在前述屏幕的宽和高范围内是可见的(矩形框与屏幕框重合算是不可见、不合理);
⑩如果上述坐标不合理,则在Draw()中用cout输出“invalid myrectangle”并换行;
⑪如果上述坐标合理,则在Draw()中用cout输出矩形的左上顶点的x、y坐标以及矩形的宽度和高度(坐标值以及宽高等4个数值间以1个空格分隔)然后换行
⑫如有必要,则增加其他数据成员及函数成员
main() 函数:
需使用如下main()函数(不得更改)
int main() {
int width, height;
cin >> width >> height;
Screen screen (width, height);
int leftX, leftY, rightX, rightY;
cin >> leftX >> leftY >> rightX >> rightY;
MyRectangle myRectangle1 (leftX, leftY, rightX, rightY, &screen);
MyRectangle* myRectangles = new MyRectangle[2];
myRectangles[1].setCoordinations(10, 300, 700, 500);
myRectangles[1].setScreen(screen);
myRectangle1.Draw();
for (int i = 0; i < 2; i++) {
myRectangles[i].setScreen(screen);
(myRectangles+i) -> Draw();
}
delete [] myRectangles;
return 0;
}
输入格式:空格分隔的整数
输出格式:
字符串或者空格分隔的整数
输入样例:
800 600
30 20 300 200
输出样例:
invalid screen size
screen
rectangle
invalid rectangle
10 300 690 300
算法如下:
#include <iostream>
#include <cstdlib>
using namespace std;
class Screen
{
public:
int width;
int height;
Screen()
{
width = 640;
height = 480;
cout << "screen" << endl;
}
Screen(int width, int height)
{
if (width > 1000 || height > 1000)
{
cout << "invalid screen size" << endl;
exit(0);
}
if(width <= 0 || height<=0 )
{
cout << "invalid screen size" << endl;
exit(0);
}
this->width = width;
this->height = height;
cout << "screen" << endl;
}
};
class MyRectangle
{
public:
int leftX;
int leftY;
int rightX;
int rightY;
Screen* p;
MyRectangle(int leftX, int leftY, int rightX, int rightY, Screen* screen):p(screen)
{
this->leftX = leftX;
this->leftY = leftY;
this->rightX = rightX;
this->rightY = rightY;
cout << "myrectangle" << endl;
}
MyRectangle()
{
this->leftX = 0;
this->leftY = 0;
this->rightX = 0;
this->rightY = 0;
cout << "myrectangle" << endl;
}
void setCoordinations(int leftX, int leftY, int rightX, int rightY)
{
this->leftX = leftX;
this->leftY = leftY;
this->rightX = rightX;
this->rightY = rightY;
}
void setScreen(Screen& screen)
{
p = &screen;
p->width = screen.width;
p->height = screen.height;
}
void Draw()
{
if(leftX>=0 && leftY>=0 && rightX>=0 && rightY>=0 && (rightX-leftX)>0 &&
(rightY-leftY)>0 && leftX < p->width && leftY < p->height &&
rightX < p->width && rightY < p->height)
{
cout << leftX << " "<< leftY << " " << (rightX-leftX) << " " << (rightY-leftY) << endl;
}
else
{
cout << "invalid myrectangle" << endl;
}
}
};
int main()
{
int width, height;
cin >> width >> height;
Screen screen (width, height);
int leftX, leftY, rightX, rightY;
cin >> leftX >> leftY >> rightX >> rightY;
MyRectangle myRectangle1 (leftX, leftY, rightX, rightY, &screen);
MyRectangle* myRectangles = new MyRectangle[2];
myRectangles[1].setCoordinations(10, 300, 700, 500);
myRectangles[1].setScreen(screen);
myRectangle1.Draw();
for (int i = 0; i < 2; i++)
{
myRectangles[i].setScreen(screen);
(myRectangles+i) -> Draw();
}
delete [] myRectangles;
return 0;
}