考虑创建一个绘图程序。需要有一个类Screen用来表示绘图时所用的屏幕 这个屏幕有一些基本属性,比如宽和高;有一些基本操作,比如获取屏幕的宽和高(10分)

题目内容:

1. Screen类有两个私有的int型数据成员,分别代表屏幕的宽和高

2. Screen类有两个构造函数

    1. 有两个整型参数,分别是屏幕的宽和高(以像素为单位)

    2. 有参构造函数将屏幕的宽和高存储在类的私有数据域中

          Screen类的有参构造函数

          Screen类的默认构造函数将屏幕宽和高分别设置为640和480

    3. Screen类的所有构造函数均应输出字符串“screen”并换行

    4. 代码中的换行需使用 cout::endl

3. 为私有数据成员提供getter和setter函数,如有必要,则增加其他数据成员及函数成员。函数原型如下

 

  1. int getWidth();
  2. int getHeight();
  3. int setWidth(int width);    //return width
  4. int setHeight(int height);  //return height

 

 

 

4. 代码所用的主函数如下(不得做任何更改):

 

  1. int main() {
  2.   int width, height;
  3.   std::cin >> width >> height;
  4.   Screen screen1 (width, height);
  5.   Screen screen2;
  6.    
  7.   screen2.setWidth(800);
  8.   screen2.setHeight(600);
  9.    
  10.   std::cout << screen1.getWidth() << ' ' << screen1.getHeight() << std::endl;
  11.   std::cout << screen2.getWidth() << ' ' << screen2.getHeight();
  12.    
  13. #ifdef DEBUG
  14.   std::cin.get();
  15. #endif
  16.   return 0;
  17. }

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值