C++ 运算符重载练习题目

1

练习数组下标运算符重载(10分)

题目内容:

 

在下面提供的MyCircle类中添加对数组下标运算符的重载。

要求:

  1. 按照数组下标由小到大,数组下标运算符按照次序分别返回圆心x坐标,圆心y坐标,圆的半径。

  2. 下标超出范围,则返回带符号整型数中最小的值

  3. 可以为MyCircle类添加数据域成员并且修改其函数成员。

  4. 但是不能修改主函数代码

 

相关代码:

 

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. class MyShape {
  6. protected:
  7.   int R_ , G_ , B_;
  8.   string colorToString() {
  9.     stringstream ss;
  10.     ss << R_ << " " << G_ << " " << B_;
  11.     return ss.str();
  12.   }
  13. public:
  14.   void setColor(int R , int G , int B) {
  15.     R_ = R; G_ = G , B_ = B;
  16.   }
  17.   int getR() {
  18.     return R_;
  19.   }
  20.   int getG() {
  21.     return G_;
  22.   }
  23.   int getB() {
  24.     return B_;
  25.   }
  26.   virtual void Draw() = 0;
  27.   MyShape() {
  28.     R_ = 255; G_ = 255 , B_ = 255;
  29.   }
  30. };
  31. class MyCircle : public MyShape {
  32. private:
  33.   int x_ = 200 , y_ = 200 , radius_ = 200;
  34. public:
  35.   MyCircle(int x , int y , int radius) {
  36.     x_ = x;
  37.     y_ = y;
  38.     radius_ = radius;
  39.   }
  40.   MyCircle() = default;
  41.   MyCircle(MyCircle& aCircle) {
  42.     x_ = aCircle.x_;
  43.     y_ = aCircle.y_;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值