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)

Source: Internet
Author: User

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")

  1. int Main () {
  2. int width, height;
  3. std::cin >> width >> height;
  4. Screen screen1 (width, height);
  5. Screen screen2;
  6. Screen2.setwidth (+);
  7. Screen2.setheight (+);
  8. std::cout << screen1.getwidth () << " << screen1.getheight () << Std::endl;
  9. std::cout << screen2.getwidth () << ' << screen2.getheight ();
  10. #ifdef DEBUG
  11. std::cin.get ();
  12. #endif
  13. return 0;
  14. }

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.