Introduction to the _c++ programming of NetEase Cloud Classroom (top) _ 6th unit: Dan maple, Although the old Jewish polymorphism – inheritance and polymorphism _ 6th Unit Job "2"-online Programming (difficulty: Medium)

Source: Internet
Author: User
Tags uppercase letter

Unit 6th Job "2"-online programming (difficulty: Medium)

View Help

return

Warm tips:

1. This assignment belongs to the online Judge topic and is enigmatic grading by the system immediately after Submission.

2. Students can submit an unlimited number of answers before the deadline , and the system will take the highest score as the final Result.

The inheritance relationship between classes is implemented on the basis of unit 5th job "4" and the code of "1" of this unit job.

In accordance with the terms of academic integrity, I guarantee that this work is done independently.

1 with Myshape as the base class, modify the MyRectangle class and the Mycircle class to derive from Myshape (10 points)

Topic Difficulty: Medium

Topic content:

Modifications based on the code for this unit job "1" and the MyRectangle class and Mycircle class for unit 5th job "4"

Modifying the Myshape class

    1. Add a member Showshape () to move all the code in the draw () function body to the Showshape () function

    2. Modify the declaration of the draw () function to make it a pure virtual function

    3. Modify the constructor of the myshape according to the requirements of the MyRectangle class and the Mycircle class below

To modify the MyRectangle class:

    1. Inheriting the MyShape class in public form

    2. Remove all data members and function members that have been moved to the Myshape class

    3. To modify all constructors
      1) enable it to set the value of the private member Type_ in the base class to the string "myrectangle"
      2) preserve the code that initializes the rectangle coordinates
      3) if necessary, Initializes a data member that has been moved to the base class through the base class constructor and the base class member function
      4) Delete all output statements in the constructor

    4. Modify the Draw () function of the MyRectangle class so that it does only the following actions:
      1) first call the base class member function Showshape ()
      2) then output the upper-left vertex of the rectangle and the rectangle width and height and wrap. The original code is preserved here, and the output format is the same as unit fifth job "4"

    5. Remove the Showscreen () function

    6. Add additional data members and function members, if necessary

    7. Do not output any information that does not require output, or it will cause the system to Buckle.

To modify the Mycircle class:

    1. Inheriting the MyShape class in public form

    2. Remove all data members and function members that have been moved to the Myshape class

    3. To modify all constructors
      1) enable it to set the value of the private member Type_ in the base class to the string "mycircle"
      2) retain code for center coordinates and RADIUS initialization
      3) if necessary, Initializes a data member that has been moved to the base class through the base class constructor and the base class member function
      4) Delete all output statements in the constructor

    4. Modify the Draw () function of the Mycircle class so that it does only the following actions:
      1) first call the base class member function Showshape ()
      2) then output center coordinates, radius, and Line wrap. The original code is preserved here, and the output format is the same as unit fifth job "4", which remains unchanged

    5. Remove the Showscreen () function

    6. Add additional data members and function members, if necessary

    7. Do not output any information that does not require output, or it will cause the system to Buckle.

Main () function:

You need to use the following main () function and helper class helpers (no changes)

  1. Template <typename t>
  2. struct Helper
  3. {
  4. typedef CHAR smalltype;
  5. typedef INT largetype;
  6. Template <typename u>
  7. static char Test (U (*) [1]);
  8. Template <typename u>
  9. static int Test (...);
  10. const Static bool Result = sizeof(test<t> (NULL)) = = sizeof(largetype);
  11. };
  12. int main () {
  13. int width, height;
  14. cin >> width >> height;
  15. screen *screen = screen::getinstance (width, height);
  16. if (! Helper<myshape>::result) cout << endl;
  17. MyShape *s1, *s2;
  18. S1 = new myrectangle ();
  19. S1->setscreen (*screen);
  20. s1->setcolor (0, 0, 0xff);
  21. s2 = new mycircle (100,110,50, screen);
  22. S1->draw ();
  23. S2->draw ();
  24. Delete s1, s2;
  25. screen->deleteinstance ();
  26. #ifdef DEBUG
  27. Std::cin.get ();
  28. #endif
  29. return 0;
  30. }

Input Format:

A space-delimited integer representing the screen width and height

Output format:

String

Input sample:

600 400

Sample Output:

Enter screen

[600x400]myrectangle (0,0,255)

10 10 90 90

[600x400]mycircle (255,255,255)

100 110 50

Leave screen

Note: There are 7 rows in the output sample, and the last behavior is a blank line

Ege Drawing Exercises Supplemental Instructions

When you finish this homework, this Season's course is coming to an end and the final exam is coming.

however, I would like you to continue to add the code of the online programming to the Ege drawing function after class so that the graphics are actually drawn on the Screen.

You are bound to meet all kinds of difficulties. But success belongs only to those who can overcome the Difficulties.

time limit: 500ms memory limit: 32000kb
#include <iostream> #include <string>using namespace std;class screen{public:int getwidth (); int getheight ( );p Ublic:~screen ();//1. adds a destructor for the screen class static screen* getinstance (int width, int height); in the screen class, add a static public getinstance (int width, int Height) function, which returns the value of Instance. Two parameters have default values of 640 and 480void deleteinstance ();//2. in the screen class, add a deleteinstance () function private:int the width int of the width_;// Height_;//high std::string enter;//1. in the screen class, add two data members of type string, enter and leave, and in the constructor, initialize them to the string "enter screen" and " Leave screen "(only one space in each string separates two Words) std::string leave;//1. in the screen class, add two string-type data members, enter and leave, And in the constructor, they are respectively initialized to the string "enter screen" and "leave screen" (only one space in each string separated by two words) static screen* instance;//2. in the screen class, add a Static private data member Instance;private:screen of type screen* (int width, int height)//converts All constructors in the screen class into private member void Exitwheninvalidscreen (int width, int height),//detects Whether the width and height of the screen conform to the Logic};int screen::getwidth () {return width_;} int screen::getheight () {return height_;} Screen::~screen ()//1. adding destructors to the screen class{std::cout << leave << std::endl;//destructors should first output the contents of a data member leave and wrap it (using std::endl)} screen* screen::getinstance (int width = 640, int height = 480)//3. in the screen class, add a static public getinstance (int width, int Height) Letter Number that returns the value of the Instance.  All two parameters have default values of 640 and 480{//4. in the getinstance function, determine the value of the INSTANCE//1) if the instance value is 0 (that is, "instance is a null pointer")// A. with width and height as parameters for the constructor, use the new operator to create a screen object//b. save the address of the new Screen object in Instance;//2) If the value of instance is not 0 (that is, instance points to a screen object), the value of instance is returned if (!instance) {instance = new screen (width, height);} Return instance;} void Screen::d eleteinstance ()//2. in the screen class, add a deleteinstance () function {delete instance;//function: getinstance () The memory requested in the function is returned to the operating System. Instance = 0;//sets The data member instance to a null pointer}screen::screen (int width, int height)//converts All constructors in the screen class to private members {// Modify the constructor of the screen class//1) delete the default constructor of the screen class, keep only the parameter constructor, and modify it to initialize the data members//2) Delete the 4th unit job in all constructors of the screen class "cout <<" screen "& lt;< endl; "statement///3) all constructors of the screen class should output the string content in the data member enter and wrap it (using std::endl), but" no longer output additional information "//4) the screen Class's constructor still uses the exit required by the IV unit jobThe Wheninvalidscreen function checks the validity of the screen width and height. (you can directly reuse the relevant code of the Iv-unit job); this part of the code must be placed after the code of the output data member enter Width_ = Width;height_ = Height;enter = "enter screen"; in the screen class, Add two data members of type string, enter and leave, and in the constructor initialize them to the string "enter screen" and "leave screen" (only one space in each string separates two words) leave = "leave Screen ";//1. in the screen class, add two data members of type string, enter and leave, and in the constructor initialize them to the string" enter screen "and" leave screen "respectively. (only one space in each string separates two Words) std::cout << enter << std::endl;exitwheninvalidscreen (width, height);//detect whether the width and height of the screen Logical}void screen::exitwheninvalidscreen (int width, int height)//detects Whether the width and height of the screen conform to the logic {if (width <= 0 | | Height <= 0)//width and height The degree 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 these conditions, the entire program simply outputs the string "invalid screen size", Then exit the 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);} }class Myshape{public://3.myshape class Constructor://1) If required, add the necessary constructor//2) all constructors should initialize the color to white, that is, the RGB three color components have a value of 255MyShape ( Screen *screen); MyShape (std::string string1);MyShape (std::string string1, Screen *screen); MyShape ();//1. Color processing//1) move a data field member that represents a color in MyRectangle and mycircle classes to the private area of the MyShape class//2) setcolor (int R, int G, int B) The function is moved to the Myshape class;//3) moves the getter function of the color data member into the Myshape class, and if none of the getter functions are in the original MyRectangle and mycircle, add it in the Myshape class. void SetColor (int R, int G, int B);//2. screen handling//1) move members of mycircle types in MyRectangle and screen* classes into the Myshape class// 2) Move the Setscreen function to the Myshape class void Setscreen (screen& screen),//5. add a function draw () in the Myshape class, just implement in the Draw () function (do not superfluous):// 1) first output the width and height of the screen, the middle with the uppercase letter "X" connected, and placed in a pair of parentheses, the shape Of: [640x480]//2) immediately after the output data member Type_ VALUE,//3) followed by the output using "half-width comma" separated three Colors. These three colors are placed in a pair of "half-width parentheses", such as: (255, 155, 55), which does not contain any spaces//3) and the last to wrap with Std::endl. virtual void Draw () = 0;//modify the Myshape Class 2. Modify the declaration of the Draw () function to make it a pure virtual function void Showshape ();//modify Myshape Class 1. Add a member Showshape () to Draw ( All code in the function body is moved to the Showshape () function private:int r_, g_, b_; screen* screen_;//4. Add a string of type "private" data member Type_//1) in the constructor initialization list of MyShape to initialize it to the string "MyShape"//2) by your own discretion Type_ access control properties, As long as it is not public access attribute can std::string type_;}; Myshape::myshape (screen *screen): r_ (255), g_ (255), b_ (255), type_ ("myshape") {screen_ = screen;} Myshape::myshape (std::string string1): r_ (255), g_ (255), b_ (255), type_ (string1) {}myshape::myshape (std::string string1, screen *screen): r_ (255), g_ (255), b_ (255), type_ (string1) {screen_ = screen;} Myshape::myshape (): r_ (255), g_ (255), b_ (255), type_ ("MyShape") {}void myshape::setcolor (int R, int G, int B) {r_ = R; G_ = G, B_ = B;} void Myshape::setscreen (screen& Screen) {screen_ = &screen;} void Myshape::showshape ()//modify MyShape Class 1. Add a member Showshape () to move all the code in the draw () function body to the Showshape () function {std::cout << [ "<< screen_->getwidth () <<" X "<< screen_->getheight () <<"] "<< type_ <<" ("& lt;< r_ << "," << g_ << "," << b_ << ")" << std::endl;} Class Myrectangle:p ublic MyShape {//modify myrectangle class: 1. Inheriting MyShape class in public form Private:int x1_, y1_, x2_, y2_;int getwidth () {retu RN x2_-x1_;} int getheight () {return y2_-y1_;} Public:myrectangle (): MyShape ("myrectangle") {//modify MyreCtangle class: 3. Modify all constructors//1) so that it is possible to set the value of the private member Type_ in the base class to the string "myrectangle"//2) to preserve the CODE//3 for the initialization of the rectangle coordinates) if necessary, The base class constructor and the base class member function initialize the data members that have been moved to the base class//4) Delete all output statements in the constructor x1_ = y1_ = 10;x2_ = Y2_ = 100;} void setcoordinations (int x1, int y1, int x2, int y2) {x1_ = x1;y1_ = y1;x2_ = X2;y2_ = y2;} void Draw () {//modify myrectangle class: 4. Modify the Draw () function of the MyRectangle class so that it only does the FOLLOWING://1) first call the base class member function Showshape ()// 2) then output the upper-left vertex of the rectangle and the rectangle width and height and wrap. The original code is preserved here, the output format is the fifth unit job "4" showshape (), cout << x1_ << "<< y1_ <<" <<this->getwidth ( ) << "" <<this->getheight () << endl;};  Class Mycircle:p ublic MyShape {//modify mycircle class: 1. Inherit MyShape class in public form Private:int x_, y_, radius_;public:mycircle (int x, int y, int radius, screen* screen): MyShape ("mycircle", screen) {//modify mycircle class: 3. Modify all constructor//1) to enable it to set the value of the private member Type_ in the base class to the string " Mycircle "//2" retains CODE//3 for center coordinates and RADIUS Initialization) if necessary, initialize the data members that have been moved to the base class by using the base class constructor and the base class member FUNCTION//4) Delete all output statements in the constructor x_ = X;y_ = Y;radius_ = radius;} void setcenter (int x, int y) {x_ = X;y_ = y;} void Setradius (int Radius) {radIus_ = radius;} void Draw () {//modify mycircle class: 4. Modify the Draw () function of the Mycircle class so that it only does the FOLLOWING://1) first calls the base class member function Showshape ()//2) and then outputs the center coordinate, radius, and Line wrap. Keep the original code here, the output format with unit fifth job "4", remain unchanged showshape (); cout << x_ << "<< y_ <<" << radius_ <&lt ; endl;}}; screen* screen::instance;//8. do not forget to initialize all static members of the screen class outside of the class, or the compiler will report a link Error. Template <typename t>struct helper{typedef char smalltype;typedef int largetype;template <typename U>static Char test (U (*) [1]); template <typename u>static int test (...); Const static BOOL Result = sizeof (test<t> (NULL)) = = sizeof (largetype);}; int main () {int width, height;cin >> width >> height; Screen *screen = screen::getinstance (width, height); Helper<myshape>::result) cout << endl;  MyShape *s1, *s2;s1 = new MyRectangle (), s1->setscreen (*screen), s1->setcolor (0, 0, 0xff), s2 = new Mycircle (100, 110, S1->draw (), s2->draw ();d elete s1, s2;screen->deleteinstance (); #ifdef debugstd::cin.get (); EndifretuRN 0;} 

Introduction to the _c++ programming of NetEase Cloud Classroom (top) _ 6th unit: Dan maple, Although the old Jewish polymorphism – inheritance and polymorphism _ 6th Unit 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.