万类霜天竞自由 – 对象和类的更多内容

题目内容:
修改Screen类:
在Screen类中,将保存屏幕宽和高的数据域成员改为 static int 类型。
在Screen类中,添加两个 string 类型的数据成员 enter 和 leave,并在构造函数中将他们分别初始化为字符串“enter screen”和“leave screen”
在Screen类中,增加一个 Screen* 类型的静态的私有数据成员 instance;
在Screen类中,增加一个静态公有的 getInstance(int width, int height) 函数,该函数返回instance的值。两个参数均带有默认值,分别为 640 和 480
在getInstance函数中,判断instance的值,若为0,则设置屏幕的高和宽,然后创建一个存放在堆区的Screen对象,将地址保存在instance中;若instance的值不为0,则返回instance的值
在getInstance函数中,如果需要创建Screen对象,则校验宽和高的值;如果宽与高超过1000,或者其它不合理的设置则用cout输出“invalid screen size”,然后结束程序
在Screen类中,添加一个deleteInstance()函数(函数类型自行根据main()中的代码确定),将getInstance()函数中申请的内存归还给操作系统。
在Screen类中,添加一个拷贝构造函数;
Screen类的默认构造函数将屏幕宽和高分别设置为640和480
Screen类的所有构造函数均应输出字符串enter的内容并换行
为screen类添加析构函数,在其中应输出字符串leave的内容并换行
删除Screen类中的带参构造函数
将Screen类中所有的构造函数都变成 private 成员
如有必要,则增加或者修改其他数据成员及函数成员,例如数据域成员的getter与setter函数
不要忘记在类外对Screen类的所有静态成员进行初始化,否则编译器会报告链接出错。




补充说明:现在的Screen类使用了一种【设计模式】,叫做“单例模式”,可以保证在这个程序中只会有一个Screen的实例。


修改MyRectangle类:
在MyRectangle类中,增加表示颜色的数据域成员;
在MyRectangle类中,增加函数 setColor(int R, int G, int B);该函数接收三个参数,代表颜色中的Red、Green、Blue分量的大小,该函数将颜色保存在类的数据域成员中。
MyRectangle类的构造函数1接受4个整型参数
按照顺序,这4整型参数分别为矩形的左上顶点的x、y坐标,以及右下顶点的x、y坐标。(此处不做坐标有效性检查)
MyRectangle类的默认构造函数将矩形左上角顶点的坐标均设置为(10,10),将右下角定点坐标设置为(100,100)
MyRectangle类的所有构造函数均将表示颜色的数据成员初始化为白色,也就是RGB三个颜色分量的值均为255
MyRectangle类的所有构造函数均应使用cout输出字符串“myrectangle”并换行
MyRectangle类中应提供setCoordinations()用于设置对角线的左侧及右侧顶点坐标;函数的参数的含义及类型和构造函数1的前4个参数相同。
MyRectangle类的Draw()函数不再检查坐标的有效性,也不输出关于坐标无效的信息;
在Draw()中用cout输出:
b. 矩形的左上顶点的x、y坐标以及矩形的宽度和高度(坐标值以及宽高等4个数值间以1个空格分隔)然后换行;
c. 矩形的颜色的RGB分量的值,用空格分隔开的三个整数,然后换行
如有必要,则增加其他数据成员及函数成员
不要输出任何未要求输出的信息,不然会导致系统扣分。


新增MyCircle类:
在MyCircle类中,增加表示颜色的数据域成员;
在MyCircle类中,增加函数 setColor(int R, int G, int B);该函数接收三个参数,代表颜色中的Red、Green、Blue分量的大小,该函数将颜色保存在类的数据域成员中。
MyCircle类的构造函数1接受3个整型参数
按照顺序,整型参数分别为圆心的x、y坐标,以及圆的半径。(此处不检查坐标及半径的有效性)
MyCircle类的默认构造函数将圆心的坐标设置为(200,200),半径设置为100
MyCircle类的“构造函数1”与默认构造函数均将表示颜色的数据成员初始化为白色,也就是RGB三个颜色分量的值均为255
为MyCircle类添加拷贝构造函数
MyCircle类的所有非拷贝构造函数均应使用cout输出字符串“mycircle”并换行
MyCircle类中应提供setCenter(int x, int y)用于设置圆心坐标,提供setRadius(int r)用于设置圆的半径。
在Draw()中用cout输出:
a. 屏幕的宽度和高度,以空格分隔,然后换行;
b. 圆心的x、y坐标以及半径(坐标值以及半径等3个数值间以1个空格分隔)然后换行;
c. 圆的颜色的RGB分量的值,用空格分隔开的三个整数,然后换行
如有必要,则增加其他数据成员及函数成员
不要输出任何未要求输出的信息,不然会导致系统扣分。


main() 函数:

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

int main() {
  int width, height;
  cin >> width >> height;
  Screen::getInstance(width, height);
  Screen::getInstance();
   
  int leftX, leftY, rightX, rightY;
  cin >> leftX >> leftY >> rightX >> rightY;
  MyRectangle myRectangle(leftX, leftY, rightX, rightY);
  myRectangle.setColor(0, 0, 0xff);
  myRectangle.Draw();
 
  int centerX, centerY, radius;
  cin >> centerX >> centerY >> radius;
  MyCircle myCircles[2];
  (myCircles + 1)->setCenter(centerX, centerY);
  myCircles[1].setRadius(radius);
  myCircles[0].Draw();
  myCircles[1].Draw();
  
  MyCircle myCircle(myCircles[1]);
  myCircle.Draw();
   
  Screen::deleteInstance();
   
  cin.get();
  return 0;
}

输入格式:
空格分隔的整数


输出格式:
字符串或者空格分隔的整数


输入样例:
800 600
30 20 300 200
10 10 50


输出样例:
invalid screen size
enter screen
leave screen
myrectangle
mycircle
10 300 690 300
255 255 255

算法如下:

#include <iostream>
#include <cstdlib>
using namespace std;

class Screen{
private:
	static Screen* instance;
	
	Screen(){
		width_ = 640;
		height_ = 480;
		enter = "enter screen";
		leave = "leave screen";
		cout <<enter<< endl;
	}
	
	Screen(Screen& C){
		cout<<enter<<endl;
	}
public:	
	static int width_;
	static int height_;
	string enter, leave;	

	static Screen* getInstance(int width = 640, int height = 480){  
		if (instance == 0){
        	instance->width_ = width;
        	instance->height_ = height;
        	if (width>1000||height>1000||width<=0||height<=0){
            	cout<<"invalid screen size"<<endl;
            	exit(0);
            }    
       		instance = new Screen;
    	}
    	else{
    	    return instance;
    	}
    }
    
    static void deleteInstance(){
    	delete instance;
	}
	
    ~Screen(){
    	cout<<leave<<endl;
	}
};
	
class MyRectangle{
public:
	int leftX;
	int leftY;
	int rightX;
	int rightY;
	Screen* p;
	int Red; 
	int Green;
	int Blue;
	
	MyRectangle(int leftX, int leftY, int rightX, int rightY){
		this->leftX = leftX;
		this->leftY = leftY;
		this->rightX = rightX;
		this->rightY = rightY;
		Red = 255;
		Green = 255;
		Blue = 255;
		cout << "myrectangle" << endl;	
	}
	
	MyRectangle(){
		this->leftX = 10;
		this->leftY = 10;
		this->rightX = 100;
		this->rightY = 100;
		Red = 255;
		Green = 255;
		Blue = 255;
		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(){
		cout << leftX << " "<< leftY << " " << (rightX-leftX) << " " << (rightY-leftY) << endl;
		cout << Red << " " << Green << " " << Blue << endl;	
	}
	
	void setColor(int R, int G, int B){
		Red = R;
		Green = G;
		Blue = B;
	}
};

class MyCircle{
public:
	int Red_;
	int Green_;
	int Blue_;
	int X;
	int Y;
	int R;
	
	MyCircle(){
		X = 200;
		Y = 200;
		R = 100;
		Red_ = 255;
		Green_ = 255;
		Blue_ = 255;
		cout << "mycircle" << endl;
	}
	
	MyCircle(int x, int y, int r){
		X = x;
		Y = y;
		R = r;
		Red_ = 255;
		Green_ = 255;
		Blue_ = 255;
		cout << "mycircle" << endl;
	}

	MyCircle(MyCircle& C){
		X = C.X;
		Y = C.Y;
		R = C.R;
		Red_ = C.Red_;
	 	Green_ = C.Green_;
	 	Blue_ = C.Blue_;
	}
	
	void setColor(int R, int G, int B){
		Red_ = R;
		Green_ = G;
		Blue_ = B;
	}
	
	void setCenter(int x, int y){
		X = x;
		Y = y;
	}
	
	void setRadius(int r){
		R = r;
	}
	
	void Draw(){
		cout << Screen::width_ << " " << Screen::height_ << endl;
		cout << X << " " << Y << " " << R <<endl;
		cout << Red_ << " " << Green_ << " " << Blue_ << endl;			
	}
};

	int Screen::width_ = 640;
	int Screen::height_ = 480;
	Screen* Screen::instance = 0;
	
int main() {
  	int width, height;
  	cin >> width >> height;
  	Screen::getInstance(width, height);
  	Screen::getInstance();
   
  	int leftX, leftY, rightX, rightY;
  	cin >> leftX >> leftY >> rightX >> rightY;
  	MyRectangle myRectangle(leftX, leftY, rightX, rightY);
  	myRectangle.setColor(0, 0, 0xff);
  	myRectangle.Draw();
 
  	int centerX, centerY, radius;
  	cin >> centerX >> centerY >> radius;
  	MyCircle myCircles[2];
  	(myCircles + 1)->setCenter(centerX, centerY);
  	myCircles[1].setRadius(radius);
  	myCircles[0].Draw();
  	myCircles[1].Draw();
  
	MyCircle myCircle(myCircles[1]);
	myCircle.Draw();
   
	Screen::deleteInstance();
   
  	cin.get();
  	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值