C++面向对象类初步实现屏幕类与矩形类

#include<iostream>
#include<cstdlib>
class Screen
{
	private:
		int width;
	    int height;

	public:
		int setWidth(int width_);
	    int setHeight(int height_);
	    int getWidth();
	    int getHeight();
	    Screen(int width_,int height_);
	    Screen();
        void exitWhenInvalidScreen(int width_, int height_);//检测屏幕的宽与高是否 符合逻辑
	
	
};

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

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

int Screen::setHeight(int height_)
{
	height=height_;
} 

int Screen::setWidth(int width_)
{
	width=width_; 
}

Screen::Screen()
{
	std::cout<<"screen"<<std::endl;
	width=640;
	height=480;
}
Screen::Screen(int width_,int height_)
{
	exitWhenInvalidScreen(width_,height_);
	std::cout<<"screen"<<std::endl;
	width=width_;
	height=height_;
}
void Screen::exitWhenInvalidScreen(int width_,int height_)
{
	if(width_<=0||width_>1000||height_<=0||height_>1000)
	{
		std::cout<<"invalib screen size";
		exit(0);
	}
}

class MyRectangle
{
	private:
		Screen* screen;
		int x1;
		int y1;
		int x2;
		int 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_); //用于设置该类的实例所对应的Screen对象
		void Draw();
};
MyRectangle::MyRectangle()
{
	std::cout << "myrectangle" << std::endl;
	x1=0;
	y1=0;
	x2=0;
	y2=0;
}
MyRectangle::MyRectangle(int x1_,int y1_,int x2_,int y2_,Screen* screen_)
{
	 std::cout << "myrectangle" << std::endl;
	 x1=x1_;
	 y1=y1_;
	 x2=x2_;
	 y2=y2_;
	 screen=screen_;
}
void MyRectangle::setCoordinations(int x1_,int y1_,int x2_,int y2_)
{
	x1=x1_;
	y1=y1_;
	x2=x1_;
	y2=y2_;
}
void MyRectangle::setScreen(Screen &screen_)
{
	screen=&screen_;
}

void MyRectangle::Draw()
{
	if(x1<=0||x1>=x2||x2>=screen->getWidth())
	{
		std::cout << "invalid myrectangle" << std::endl;
        return;
	}
	if(y1<=0||y1>=y2||y2>=screen->getHeight())
	{
		std::cout << "invalid myrectangle" << std::endl;
        return;
	}
	std::cout << x1 << " " << y1<< " " << x2 - x1 << " " << y2 - y1<< std::endl;
}
int main() {
  int width, height;
  std::cin >> width >> height;
  Screen  screen (width, height);
  
  int leftX, leftY, rightX, rightY;
  std::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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值