1. 程式人生 > >網易雲課堂_C++程序設計入門(下)_第8單元:年年歲歲花相似– 運算符重載_第8單元 - 作業2:OJ編程 - 重載數組下標運算符

網易雲課堂_C++程序設計入門(下)_第8單元:年年歲歲花相似– 運算符重載_第8單元 - 作業2:OJ編程 - 重載數組下標運算符

ref [] jna turn draw cred 超出範圍 input detail

第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編程 - 重載數組下標運算符