题目内容:
1. Screen类有两个私有的int型数据成员,分别代表屏幕的宽和高
2. Screen类有两个构造函数
-
-
有两个整型参数,分别是屏幕的宽和高(以像素为单位)
-
有参构造函数将屏幕的宽和高存储在类的私有数据域中
Screen类的有参构造函数
Screen类的默认构造函数将屏幕宽和高分别设置为640和480
-
Screen类的所有构造函数均应输出字符串“screen”并换行
-
代码中的换行需使用 cout::endl
-
3. 为私有数据成员提供getter和setter函数,如有必要,则增加其他数据成员及函数成员。函数原型如下
- int getWidth();
- int getHeight();
- int setWidth(int width); //return width
- int setHeight(int height); //return height
4. 代码所用的主函数如下(不得做任何更改):
- int main() {
- int width, height;
- std::cin >> width >> height;
- Screen screen1 (width, height);
- Screen screen2;
- screen2.setWidth(800);
- screen2.setHeight(600);
- std::cout << screen1.getWidth() << ' ' << screen1.getHeight() << std::endl;
- std::cout << screen2.getWidth() << ' ' << screen2.getHeight();
- #ifdef DEBUG
- std::cin.get();
- #endif
- return 0;
- }
#include <iostream>
using namespace std;
class Screen {
private:
int width_;
int height_;
public://函数原型
int getWidth();
int getHeight();
int setWidth(int width);
int setHeight(int height);
Screen(int width, int height);//构造函数
Screen();//构造函数
};
int Screen::getWidth() {
return width_;
}
int Screen::getHeight() {
return height_;
}
int Screen::setHeight(int height) {
height_ = height;
return height;
}
int Screen::setWidth(int width) {
width_ = width;
return width;
}
//构造函数
Screen::Screen(int width, int height) {
std::cout << "Screen" << std::endl;
width_ = width;
height_ = height;
}
//默认构造函数
Screen::Screen() {
std::cout << "screen" << std::endl;
width_ = 640;
height_ = 480;
}
int main() {
int height, width;
std::cin >> width >> height;
Screen Screen1(width, height);
Screen Screen2;
Screen2.setWidth(800);
Screen2.setHeight(600);
std::cout << Screen1.getWidth() << " " << Screen1.getHeight() << std::endl;
std::cout << Screen2.getWidth() << " " << Screen2.getHeight();
std::cin.get();
return 0;
}