Unit 8th-Job 2:oj Programming-overloaded array subscript operator
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.
Practice array subscript operator overloading
In accordance with the terms of academic integrity, I guarantee that this work is done independently.
1 Practice Array subscript operator overloading (6 points)
For details of this topic, see "Unit 8th-Unit 2 description"
time limit: 500ms memory limit: 32000kb
#include <iostream> #include <limits> #include <string> #include <sstream>using namespace std; Class MyShape {Protected:int r_, g_, b_;string colortostring () {stringstream ss;ss << r_ << "" << g_ &L t;< "" << 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) {radius_ = radius;} void Draw () {}//----Add a relational operator here >, <, >=, <=, = =,! = Overloaded prototype declaration int& operator[] (const int &index);};/ /----The overloaded definition int& mycircle::operator[] (const int & index) {if (index = = 0) {return x_;}, where the relational operator is added here) else if (index = = 1) {return y_;} else if (index = = 2) {return radius_;} Else{min = Numeric_limits<int>::min ();//subscript out of range, returns the smallest value of the signed integer return min;}} int main () {int x, y, r = 0;cin >> x >> y >> R; Mycircle C1 (x, Y, R); Mycircle c2;c2[2] = x;for (int i = 0; I <= 3; i++) {cout << c1[i] << endl;cout << c2[i-1] << Endl;} return 0;}
Introduction to the _c++ programming of NetEase Cloud Classroom (Next) _ Unit 8th: Year-old flower similarity – operator Overloading _ 8th Unit-Job 2:OJ programming-overloaded array subscript operator