1
Adds a private function to the screen class to detect whether the width and height of the screens are logical (10 points)
Topic content:
Based on the job "1" in this unit, add a private function in the screen class Exitwheninvalidscreen is used to detect whether the width and height are logical
1. The return value type of the function Exitwheninvalidscreen, the number and the type of the parameter, please specify by yourself.
2. The function Exitwheninvalidscreen's judgment logic is as follows:
1) Width and height must not be greater than 1000 pixels (can be equal to 1000 pixels)
2) Width and height must be greater than 0 pixels (not equal to 0 pixels)
3) If either width or height does not satisfy any of the above conditions, the entire program simply outputs the string "Invalid screen size" and then exits the program
3. In the screen class of the parameter constructor and setter function, to call the Exitwheninvalidscreen function to detect the width and height
4. The main function in the program is the following (exactly the same as the job "1")
- 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;
- }
5. Tips
Tip 1:exit () function can be forced to quit the program, the function in the header file <cstdlib>
Tip 2: The function Exitwheninvalidscreen can be designed to have two parameters, width and height, respectively. When it is only necessary to judge whether one of the wide or sophomore is logical, a logical number can be assigned to the other parameter arbitrarily
Tip 3: When the screen width and height are not logical, only the specified string is output, do not output any excess information
Input format:
A space-delimited two integer representing the width and height of the screen
Output format:
The input data determines the content of the output.
There are two possible outputs:
The output string "Invalid screen size". Line cannot be wrapped after outputting the string
Or
Output format similar to Job "1"
Input Sample 1:
320 2400
Output Example 1:
Invalid screen size
Input Sample 2:
320 240
Output Example 2:
Screen
Screen
320 240
800 600
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 private:void exitwheninvalidscreen (int width, int height) of the screen,//detects whether the width and height of the screen are logical};int 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 {exitwheninvalidscreen (width, height);//detect whether the width and height of the screen are logical 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;} void Screen::exitwheninvalidscreen (int width, int height)//detects if the width and height of the screen conform to the logical {if (width <= 0|| Height <= 0)//width and height must be greater than 0 pixels (not equal to 0 pixels) {std::cout << "invalid screen size";//if either width or height does not satisfy any of the above conditions, the entire program simply outputs the string " Invalid screen Size ", then exit program exit (0);} if (Width > | | height > 1000)//width and height must not be greater than 1000 pixels (can be equal to 1000 pixels) {std::cout << "invalid screen size"; exit (0);} }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 "2"-online programming (difficulty: Medium)