Introduction to the _c++ programming of NetEase Cloud Classroom (Next) _ 9th unit: Baigun Zeng Yong Peony Fang, a kind of fresh shes unique "exception" _ 9th Unit-Job 5:oj programming-using Exceptions for graphics class error handling

Source: Internet
Author: User

Unit 9th-Job 5:OJ Programming-error handling of graphics classes using exceptions

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.

Add exception handling code to the graphics class based on the job contents of unit 8th

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

1 Add exception-handling code for the graphics class based on the code for unit 8th Job 2. (5 points)

Details of the topic see: Unit 9th-Assignment 5 Description: "OJ-error handling of graphics classes using exceptions"

time limit: 500ms memory limit: 32000kb

 

#include <iostream> #include <string> #include <sstream> #include <stdexcept>using namespace Std;class MyShape {protected:int r_, g_, b_;string colortostring () {stringstream ss;ss << r_ << "" << G _ << "" << B_;return Ss.str ();} Public:void setcolor (int R, int G, int B) {r_ = R; G_ = G, b_ = B;} int Getr () {return r_;} int Getg () {return g_;} int Getb () {return b_;} virtual void Draw () = 0; MyShape () {r_ = 255; G_ = 255, b_ = 255;}}; Class Mycircle:public MyShape {private:int x_, y_, radius_;int min;public:mycircle (int x, int y, int radius) {x_ = X;y_ = Y;radius_ = radius;} Mycircle () {x_ = Y_ = 200;radius_ = 100;} Mycircle (mycircle& acircle) {x_ = Acircle.x_;y_ = Acircle.y_;radius_ = Acircle.radius_; R_ = Acircle.getr (); G_ = ACIRCLE.GETG (); B_ = Acircle.getb ();} void SetCenter (int x, int y) {x_ = X;y_ = y;} void Setradius (int radius) {//3. Modify the Setradius (int radius) function, which throws a Underflow_error exception when the radius value is less than or equal to 0 o'clock. 3.1 Note: The value of radius is checked only in the Setradius function;Other functions, including Myshape's constructor, do not check that the value of radius is legitimate//3.2 the contents of the string that is passed to its constructor when the Underflow_error exception object is created is: "Radius Underflow:radius ", (does not contain double quotes). Where "radius" is an example of a parameter//2.3 string content that is accepted by the Setradius () function When an exception occurs: "Radius underflow: 100" (The actual output does not contain double quotes; the words are separated by a space; the colon is a half-width character, and no space before the colon) There is a space between the colon and the subsequent number. -100 is the parameter passed to the Setradius () function) if (Radius > 0) {radius_ = radius;} Else{stringstream ss;ss << "Radius underflow:" << radius;throw Underflow_error (Ss.str ());}} void Draw () {}//----Add a relational operator here >, <, >=, <=, = =,! = Overloaded prototype declaration int& operator[] (const int &index);};/ /----Add the overloaded definition of the relational operator here int& mycircle::operator[] (const int & index) {//2. Modifies the overloaded code of the array subscript operator, which is thrown when the current label is out of range Range_ Error exception. (The smallest value in a signed integer is no longer returned)//2.1 when you create the Range_error exception object, the content of the string//2.2 is passed to its constructor: "Index exceeds Scope:index" (without double quotes). The "index" is an example of a parameter//2.3 string content that is accepted by the array subscript operator when an exception occurs: "Index exceeds Scope:3" (the actual output does not contain double quotes; the word is separated by a space; the colon is a half-width character, and no space before the colon) There is a space between the colon and the following number. The number 3 is the parameter passed to the array subscript operator) if (index = = 0) {return x_;} else if (index = = 1) {return y_;} else if (index = = 2) {return radIus_;} Else{stringstream ss;ss << "Index exceeds scope:" << index;throw Range_error (Ss.str ());}} int main () {int r1 = 0, r2 = 0;cin >> R1 >> R2; Mycircle C1, C2;try {C1.setradius (r1); C2.setradius (R2); for (int i = 0; I <= 3; i++) {cout << c1[i] << Endl; }}catch (underflow_error& e) {cout << e.what () << Endl;} catch (runtime_error& e) {cout << e.what () << Endl;} try {c2[r1/(r2 = = 0? 1:r2)] = 321;for (int i = 3; I >= 0; i--) {cout << c2[i-1] << Endl;}} catch (exception& e) {cout << e.what () << Endl;} GCC and VC compilers are paused in debug mode for easy viewing of Run results # if (defined (__debug__) | | defined (_DEBUG)) Cin.ignore (numeric_limits<streamsize >::max (), ' \ n '); Cin.get (); #endifreturn 0;}

Introduction to the _c++ programming of NetEase Cloud Classroom (Next) _ 9th unit: Baigun Zeng Yong Peony Fang, a kind of fresh shes unique "exception" _ 9th Unit-Job 5:oj programming-using Exceptions for graphics class error handling

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.