面向对象程序设计入门第五单元四题练习解答
2018-01-18 14:44
162 查看
#include<iostream> #include<cstdlib> class Screen { private: int Width; int Height; std::string enter; std::string leave; static Screen *instance; void exitWhenInvalidScreen(int width,int height) { if(width>1000||height>1000||width<=0||height<=0) { std::cout<<"invalid screen size"<<std::endl; exit(0); } }; Screen(int width,int height) { Width=width; Height=height; enter="enter screen"; leave="leave screen"; std::cout<<enter<<std::endl; exitWhenInvalidScreen(width,height); }; public: ~Screen() { std::cout<<leave<<std::endl; }; void deleteInstance() { delete instance; instance=0; }; int getWidth() { return Width; }; int getHeight() { return Height; }; /*int setWidth(int width) { exitWhenInvalidScreen(width,500); Width=width; return Width; }; int setHeight(int height) { exitWhenInvalidScreen(500,height); Height=height; return Height; };*/ static Screen* getInstance(int width=640,int height=480) { if (!instance) { instance = new Screen(width, height); } return instance; }; }; Screen* Screen::instance; class MyRectangle { private: int x1_, y1_, x2_, y2_; Screen* screen_; int red,green,blue; int getWidth() { return x2_ - x1_; } int getHeight() { return y2_ - y1_; } public: MyRectangle (int x1, int y1, int x2, int y2, Screen* screen) { x1_ = x1; y1_ = y1; x2_ = x2; y2_ = y2; screen_ = screen; red=green=blue=255; std::cout << "myrectangle" <<std::endl; } MyRectangle () { x1_ = y1_ =10; x2_ = y2_=100; screen_ = 0; red=green=blue=255; std::cout << "myrectangle" <<std::endl; } void setCoordinations(int x1, int y1, int x2, int y2) { x1_ = x1; y1_ = y1; x2_ = x2; y2_ = y2; } void setScreen(Screen& screen) { screen_ = &screen; } void Draw () { std::cout << x1_ << " " << y1_ << " " << this->getWidth() << " " << this->getHeight() <<std::endl; std::cout<<red<<" "<<green<<" "<<blue<<std::endl; } void setColor(int R, int G, int B) { red=R; green=G; blue=B; } void showScreen() { std::cout<<screen_->getWidth()<<" "<<screen_->getHeight()<<std::endl; } }; class MyCircle { private: int Red,Green,Blue; int x_,y_,r_; Screen* screen_; public: void setColor(int R,int G,int B) { Red=R; Green=G; Blue=B; } MyCircle(int x,int y,int r,Screen* screen) { screen_=screen; x_=x; y_=y; r_=r; Red=Green=Blue=255; std::cout << "mycircle" << std::endl; } MyCircle() { x_=200; y_=200; r_=100; Red=255; Green=255; Blue=255; std::cout << "mycircle" << std::endl; } MyCircle(const MyCircle& C) { x_=C.x_; y_=C.y_; r_=C.r_; Red=C.Red; Green=C.Green; Blue=C.Blue; screen_=C.screen_; std::cout<<"copy mycircle"<<std::endl; } void setCenter(int x,int y) { x_=x; y_=y; } void setRadius(int r) { r_=r; } void Draw() { std::cout<<x_<<" "<<y_<<" "<<r_<<std::endl; std::cout<<Red<<" "<<Green<<" "<<Blue<<std::endl; } void showScreen() { std::cout<<screen_->getWidth()<<" "<<screen_->getHeight()<<std::endl; } void setScreen(Screen& screen) { screen_ = &screen; } }; int main() { int width, height; std::cin >> width >> height; int leftX, leftY, rightX, rightY; std::cin >> leftX >> leftY >> rightX >> rightY; int centerX, centerY, radius; std::cin >> centerX >> centerY >> radius; Screen *screen = Screen::getInstance(width, height); MyRectangle myRectangle(leftX, leftY, rightX, rightY, screen); myRectangle.setColor(0, 0, 0xff); myRectangle.showScreen(); myRectangle.Draw(); // 构造圆形对象数组 //// 第一个元素使用匿名对象(调用带参构造函数)初始化 //// 第二个元素使用匿名对象(调用默认构造函数)初始化 MyCircle myCircles[2] = { MyCircle(centerX, centerY, radius, screen) }; // 设置对象数组中第二个元素的属性。注意访问成员函数的不同方法 (myCircles + 1)->setCenter(centerX+10, centerY+20); myCircles[1].setRadius(radius+30); (*(myCircles+1)).setColor(0x00, 0x00, 0x00); myCircles[1].setScreen(*screen); for(int i=0; i<=1; i++) { myCircles[i].showScreen(); (myCircles+i)->Draw(); } // 调用拷贝构造函数以myCircles数组中的第二个元素为模板创建新对象 MyCircle yourCircle(myCircles[1]); yourCircle.showScreen(); (&yourCircle)->Draw(); screen->deleteInstance(); #ifdef DEBUG std::cin.get(); #endif return 0; }
相关文章推荐
- 第五,六,九,十二单元练习
- 第五-六单元练习
- 第五、六单元练习
- 蓝桥杯练习系统习题解答-入门训练
- 第五-六单元练习
- python编程:从入门到实践-第六章练习
- 框架 day53 BOS项目练习(权限概述,权限数据模型,shiro入门{bos中应用})
- 练习4.1 E2 解答
- 《Python核心编程》第二版第六章练习 6-14 解答
- 《Python编程:从入门到实践》课后练习6-1到6-11
- 入门教程:ASP设计常见问题及解答精要4
- 算法-第四版-练习1.2.6解答
- 算法-第四版-练习1.2.11解答
- 算法-第四版-练习1.2.17解答
- 动态规划入门练习
- 算法-第四版-练习1.3.2解答
- 蓝桥杯练习系统——入门训练
- 母函数初期简单入门题练习
- 【原创】DE2 实验练习解答—lab 3:锁存器、触发器和寄存器(digital Logic)(DE2)(quartus II)
- 【Java入门】练习示例