您的位置:首页 > 其它

面向对象程序设计入门第五单元四题练习解答

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: