zoukankan      html  css  js  c++  java
  • 网易云课堂_C++程序设计入门(上)_第4单元:物以类聚 – 对象和类_第4单元作业【1】

    1

    考虑创建一个绘图程序。需要有一个类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. }

    输入格式:

    两个由空格分隔的整数,代表屏幕的宽和高

    输出格式:

    两次调用构造函数所输出字符串,字符串后换行

    两个不同屏幕对象的宽和高,由空格分隔,第一个屏幕对象的宽和高输出后换行

    输入样例:

    320 240

    输出样例:

    screen

    screen

    320 240

    800 600

    注意 :上述输出一共4行,最后一行后面 没有 换行

    时间限制:500ms内存限制:32000kb
     
    #include <iostream>
    
    class Screen
    {
    public:
    	int getWidth();
    	int getHeight();
    	int setWidth(int width);    //return width
    	int setHeight(int height);  //return height
    public:
    	Screen(int width, int height);//有参构造函数
    	Screen();//默认构造函数
    private:
    	int width_;//屏幕的宽
    	int height_;//屏幕的高
    };
    
    int Screen::getWidth()
    {
    	return width_;
    }
    
    int Screen::getHeight()
    {
    	return height_;
    }
    
    int Screen::setWidth(int width)    //return width
    {
    	width_ = width;
    	return width;
    }
    
    int Screen::setHeight(int height)  //return height
    {
    	height_ = height;
    	return height;
    }
    
    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 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;
    }
    
  • 相关阅读:
    Linux配置环境变量
    入坑CV DL一些基础技能学习
    (Linux)初探cmake .和make命令
    Django rest framework 之分页
    Django rest framework 之版本
    通用权限框架
    堡垒机
    kingadmin
    CRM 客户关系管理系统
    Torando 入门
  • 原文地址:https://www.cnblogs.com/denggelin/p/5880680.html
Copyright © 2011-2022 走看看