码迷,mamicode.com
首页 > 编程语言 > 详细

网易云课堂_C++程序设计入门(下)_第8单元:年年岁岁花相似– 运算符重载_第8单元 - 作业2:OJ编程 - 重载数组下标运算符

时间:2017-07-08 10:04:00      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:space   include   ueditor   ons   raw   oid   limits   .com   end   

 

第8单元 - 作业2:OJ编程 - 重载数组下标运算符

返回
 

温馨提示:

1.本次作业属于Online Judge题目,提交后由系统即时判分。

2.学生可以在作业截止时间之前不限次数提交答案,系统将取其中的最高分作为最终成绩。

练习数组下标运算符重载

依照学术诚信条款,我保证此作业是本人独立完成的。

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

本题目具体内容请参见 【第8单元 - 单元作业2说明

时间限制:500ms内存限制: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_ << " " << 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() {
	}

	//----在此处添加关系运算符  >、<、>=、<=、==、!=  的重载原型声明
	int& operator[](const int &index);
};

//----在此处添加关系运算符的重载定义
int& MyCircle::operator[](const int & index)
{
	if (index == 0)
	{
		return x_;
	}
	else if (index == 1)
	{
		return y_;
	}
	else if (index == 2)
	{
		return radius_;
	}
	else
	{
		min = numeric_limits<int>::min();//下标超出范围,则返回带符号整型数中最小的值
		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;
}

 

网易云课堂_C++程序设计入门(下)_第8单元:年年岁岁花相似– 运算符重载_第8单元 - 作业2:OJ编程 - 重载数组下标运算符

标签:space   include   ueditor   ons   raw   oid   limits   .com   end   

原文地址:http://www.cnblogs.com/denggelin/p/7135772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!