1
Consider creating a drawing program. You need a screen that is used to represent a drawing when the screen has some basic properties, such as width and height, and some basic operations, such as capturing the width and height of the screen (10 points)
Topic content:
1. The screen class has two private int data members representing both the width and height
2. The screen class has two constructors
There are two integer parameters, each of which is the width and height of the screen (in pixels)
The parameter constructor stores the width and height of the screen in the private data field of the class.
The parameter constructor of the screen class
The default constructor for the screen class sets the width and height to 640 and 480, respectively.
All constructors of the screen class should output the string "screen" and wrap it.
Line breaks in code need to use Cout::endl
3. Provide getter and setter functions for private data members and, if necessary, add additional data members and function members. Function prototypes are as follows
- int getwidth ();
- int getheight ();
- int setwidth (int width); //return width
- int setheight (int height); //return height
4. The main function used by the code is as follows (no changes should be made):
- int Main () {
- int width, height;
- std::cin >> width >> height;
- Screen screen1 (width, height);
- Screen screen2;
- Screen2.setwidth (+);
- Screen2.setheight (+);
- std::cout << screen1.getwidth () << " << screen1.getheight () << Std::endl;
- std::cout << screen2.getwidth () << ' << screen2.getheight ();
- #ifdef DEBUG
- std::cin.get ();
- #endif
- return 0;
- }
Input format:
Two integers separated by spaces, representing the width and height of the screen
Output format:
Two calls to the constructor output string, string wrapping
Two different screen objects wide and high, separated by spaces, the width and height of the first screen object and the line wrapping after the output
Input Sample:
320 240
Sample output:
Screen
Screen
320 240
800 600
Note : The above output is 4 lines and there is no line break after the last line
time limit: 500ms memory limit: 32000kb
#include <iostream>class screen{public:int getwidth (); int getheight (); int setwidth (int width); return widthint setheight (int height); return heightpublic:screen (int width, int height);//Parameter constructor screen ();//default constructor private:int width;//width int height;// The high};int of the screen Screen::getwidth () {return width;} int Screen::getheight () {return height;} int screen::setwidth (int width)//return width{this->width = Width;return width;} int screen::setheight (int height)//return height{this->height = height;return height;} Screen::screen (int width, int height)//parameter constructor {std::cout << "screen" << std::endl;this->width = width; This->height = height;} Screen::screen ()//default constructor {std::cout << "screen" << std::endl;this->width = 640;this->height = 480;} int main () {int width, height;std::cin >> width >> height; Screen screen1 (width, height); Screen Screen2;screen2.setwidth (screen2.setheight); Std::cout << screen1.getwidth () << "< < SCReen1.getheight () << std::endl;std::cout << screen2.getwidth () << ' << screen2.getheight (); Ifdef debugstd::cin.get (); #endifreturn 0;}
Introduction to NetEase Cloud classroom _c++ Programming (top) _ 4th unit: Birds of a feather-objects and Classes-Unit 4th Job "1"-Online programming (difficulty: Easy)