1
练习数组下标运算符重载(10分)
题目内容:
在下面提供的MyCircle类中添加对数组下标运算符的重载。
要求:
-
按照数组下标由小到大,数组下标运算符按照次序分别返回圆心x坐标,圆心y坐标,圆的半径。
-
下标超出范围,则返回带符号整型数中最小的值
-
可以为MyCircle类添加数据域成员并且修改其函数成员。
-
但是不能修改主函数代码
相关代码:
- #include <iostream>
- #include <string>
- #include <sstream>
- 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_ = 200 , y_ = 200 , radius_ = 200;
- public:
- MyCircle(int x , int y , int radius) {
- x_ = x;
- y_ = y;
- radius_ = radius;
- }
- MyCircle() = default;
- MyCircle(MyCircle& aCircle) {
- x_ = aCircle.x_;
- y_ = aCircle.y_;