C++ 屏幕类,矩形类简单小练习

1

在本单元作业【1】和作业【2】的基础上,创建一个MyRectangle类,并在main函数中创建类的实例。(10分)

题目难度: 难

题目内容:

Screen类:

与作业【2】要求完全相同。

如果你的作业【2】顺利通过,那么你可以直接使用作业【2】中Screen类的代码

MyRectangle类:

MyRectangle 代表的是一个矩形。我们用矩形左上角和右下角两个顶点的(x,y)坐标来表示它

1. MyRectangle类中的数据域有一个唯一与Screen类有关的成员,其类型为 Screen* 类型 

 

  1. Screen* screen_;

2. MyRectangle类的带参构造函数接受5个参数,其中前4个是整型参数

   1)按照顺序,整型参数分别为矩形的左上顶点的x1、y1坐标,以及右下顶点的x2、y2坐标。(在构造函数中,不检查坐标有效性,也就是说,哪怕坐标出现负值,也不理会它。而是在后面的Draw函数中再做有效性检查)

   2)按照顺序,第5个参数为Screen类的对象指针

 

  1. //带参构造函数原型声明
  2. MyRectangle::MyRectangle(int x1, int y1, int x2, int y2, Screen* screen);

3. MyRectangle类的默认构造函数将【左上----右下】对角线两个点的坐标均设置为原点坐标(0,0)

 

  1. //默认构造函数原型声明
  2. MyRectangle::MyRectangle();

4. MyRectangle类的所有构造函数均应使用cout输出字符串“myrectangle”并换行(使用cout::endl)

 

5. MyRectangle类中应提供setCoordinations()用于设置对角线的左侧及右侧顶点坐标;该函数共有4个形式参数。这些参数的含义及类型与“带参构造函数”的前4个参数相同。该函数将形式参数的值拷贝到类的私有数据域中。

 

6. MyRectangle类中应提供setScreen(Screen& screen)用于设置该类的实例所对应的Screen对象;

    1)也就是说,setScreen函数会将引用参数 screen 这个对象的地址赋给MyRectangle类中的私有成员 screen_。

    2)要注意:私有成员 screen_ 是对象指针类型,而setScreen的函数参数 screen 是对象引用类型

    3)所以,必须要取 screen 的地址,然后将该地址赋值给私有成员 screen_

    4)函数返回值类型由你自己决定

注: 如果你学有余力,可以尝试在这一步中,将函数原型变为 setScreen(const Screen& screen),尝试解决编译错误(提示:需要修改Screen类)

7. MyRectangle类的Draw()函数应检查坐标的有效性,确保矩形的顶点坐标是合理的、在前述屏幕的宽和高范围内是可见的(矩形框与屏幕框重合算是不可见、不合理);

    1)如果上述坐标不合理,则在Draw()中用cout输出“invalid myrectangle”然后换行(用std::endl);

    2)如果上述坐标合理,则在Draw()中用cout输出矩形的左上顶点的x、y坐标以及矩形的宽度和高度(一共4个数值,任意两个相邻数值间以1个空格分隔;第4个数值后面没有空格),然后换行(用std::endl)

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

main() 函数:

需使用如下main()函数(不得更改)

 

  1. int main() {
  2.   int width, height;
  3.   cin >> width >> height;
  4.   Screen  screen (width, height);
  5.   
  6.   int leftX, leftY, rightX, rightY;
  7.   cin >> leftX >> leftY >> rightX >> rightY;
  8.   MyRectangle myRectangle1 (leftX, leftY, rightX, rightY, &screen);
  9.   MyRectangle* myRectangles = new MyRectangle[2];
  10.   
  11.   myRectangles[1].setCoordinations(10, 300, 700, 500);
  12.   myRectangles[1].setScreen(screen);
  13.     
  14.   myRectangle1.Draw();
  15.   for (int i = 0; i < 2; i++) {
  16.     myRectangles[i].setScreen(screen);
  17.     (myRectangles+i) -> Draw();
  18.   }
  19.     
  20.   delete [] myRectangles;
  21.    
  22. #ifdef DEBUG
  23.   std::cin.get();
  24. #endif
  25.   return 0;
  26. }

输入格式:

空格分隔的整数

输出格式:

字符串

或者

空格分隔的整数

输入样例:

800 600

30 20 300 200

输出样例:

screen

myrectangle

myrectangle

myrectangle

30 20 270 180

invalid myrectangle

10 300 690 200

#include <iostream> 
using namespace std;

class Screen
{
private:
	int Width;
	int Height;
	void exitWhenInvalidScreen(int height,int width);
public:
	Screen() :Width(0), Height(0)
	{
		cout << "screen" << endl;
	}
	Screen(int width, int height) :Width(width), Height(height)
	{
		exitWhenInvalidScreen(height, width);
		cout << "screen" << endl;
	}
	int getWidth();
	int getHeight();
	int setWidth(int width);
	int setHeight(int height);
};

class MyRectangle
{
private:
	Screen * screen_;
	int x1_, y1_, x2_, y2_;
public:
	MyRectangle(int x1, int y1, int x2, int y2, Screen* screen);
	MyRectangle();
	void setCoordinations(int x1, int y1, int x2, int y2);
	void setScreen(Screen& screen);
	void Draw();
};

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

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

int Screen::setHeight(int height)
{
	Height = height;
	exitWhenInvalidScreen(height, 500);
	return Height;
}

void Screen::exitWhenInvalidScreen(int height, int width)
{
	if (height > 1000 || width > 1000 || height <= 0 || width <= 0)
	{
		cout << "invalid screen size" << endl;
		exit(0);
	}
}
int Screen::setWidth(int width)
{
	Width = width;
	exitWhenInvalidScreen(500, width);
	return Width;
}

MyRectangle::MyRectangle():x1_(0),y1_(0),x2_(0),y2_(0)
{
	cout << "myrectangle" << endl;
}

MyRectangle::MyRectangle(int x1, int y1, int x2, int y2, Screen* screen):
	x1_(x1),y1_(y1),x2_(x2),y2_(y2),screen_(screen)
{
	cout << "myrectangle" << endl;
}

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

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

void MyRectangle::Draw()
{
	if (x1_ <= 0 || y1_ <= 0 || x2_ > screen_->getWidth() || y2_ > screen_->getHeight())
	{
		cout << "invalid myrectangle" << endl;
	}
	else if (x1_ >= x2_ || y1_ >= y2_)
	{
		cout << "invalid myrectangle" << endl;
	}
	else
	{
		cout << x1_ << " " << y1_ << " " << x2_ - x1_ << " " << y2_ - y1_ << 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;

#ifdef DEBUG
	std::cin.get();
#endif
	return 0;
}

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/82899357
今日推荐