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_;
  44.     radius_ = aCircle.radius_;
  45.     R_ = aCircle.getR();
  46.     G_ = aCircle.getG();
  47.     B_ = aCircle.getB();
  48.   }
  49.   void setCenter(int x , int y) {
  50.     x_ = x;
  51.     y_ = y;
  52.   }
  53.   void setRadius(int radius) {
  54.     radius_ = radius;
  55.   }
  56.   void Draw() {
  57.   }
  58.   //----在此处添加数组下标运算符的重载函数原型声明
  59. };
  60. //----在此处添加关系数组下标运算符的重载定义
  61. int main() {
  62.   int x , y , r = 0;
  63.   cin >> x >> y >> r;
  64.   MyCircle c1(x , y , r);
  65.   MyCircle c2;
  66.   c2[ 2 ] = x;
  67.   for (int i = 0; i <= 3; i++) {
  68.     cout << c1[ i ] << endl;
  69.     cout << c2[ i - 1 ] << endl;
  70.   }
  71.   return 0;
  72. }

注1:通过 numeric_limits<int>::min()  可以拿到最小的整数。该函数需要包含头文件 <limits>

注2:数组下标运算符要返回一个引用才能作为左值,但是“带符号整型数中最小的值”是个常量,不能作为函数的引用返回值。那么,可以在类中定义一个数据域成员,让它等于那个最小的值,然后返回这个数据域成员的引用,就可以做左值了。

注3:OJ使用GCC编译器。

输入格式:

空格分隔的3个整数

输出格式:

8个整数,每行一个。第8个整数后面有换行

输入样例:

1 2 3

输出样例:

1

-2147483648

2

200

3

200

-2147483648

1

第9行是空行

时间限制:500ms内存限制:32000kb

 

#include <iostream>
#include <string>
#include <sstream>
#include <limits>
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;
	int min_num = numeric_limits<int>::min();
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_;
		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 [] (int i) {
		switch (i)
		{
		case 0:
			return x_;
		case 1:
			return y_;
		case 2:
			return radius_;
		default:
			return min_num;
		}
	}
};
//----在此处添加关系数组下标运算符的重载定义
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;
}

1

在如下给定的代码中,为 MyCircle 类添加关系运算符 >、< >=、< MyCircle >(10分)

题目内容:

 

在如下给定的代码中,为 MyCircle 类添加关系运算符 >、<、>=、<=、==、!= ,实现按照半径对 MyCircle 对象进行比较。

 

要求

  1. 关系运算符重载后的返回值均为 bool 类型

  2. 不得修改所给的代码

 

  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_;
  44.     radius_ = aCircle.radius_;
  45.     R_ = aCircle.getR();
  46.     G_ = aCircle.getG();
  47.     B_ = aCircle.getB();
  48.   }
  49.   void setCenter(int x , int y) {
  50.     x_ = x;
  51.     y_ = y;
  52.   }
  53.   void setRadius(int radius) {
  54.     radius_ = radius;
  55.   }
  56.   void Draw() {
  57.   }
  58.   //----在此处添加关系运算符  >、<、>=、<=、==、!=  的重载原型声明
  59. };
  60. //----在此处添加关系运算符的重载定义
  61. int main() {
  62.   int r1 , r2 , r3 = 0;
  63.   cin >> r1 >> r2 >> r3;
  64.   MyCircle c1 , c2 , c3;
  65.   c1.setRadius(r1);
  66.   c2.setRadius(r2);
  67.   c3.setRadius(r3);
  68.   cout << (c1 > c2) << endl;
  69.   cout << (c1 < c2) << endl;
  70.   cout << (c2 >= c3) << endl;
  71.   cout << (c2 <= c3) << endl;
  72.   cout << (c1 == c3) << endl;
  73.   cout << (c1 != c3) << endl;
  74.   return 0;
  75. }

 

输入格式:

空格分隔的3个整数

 

输出格式:

6个整数,每行一个

整数只限0或者1

 

输入样例:

8 9 1

 

输出样例:

0

1

1

0

0

1

一共6行。最后一行后面没有换行符

时间限制:500ms内存限制:32000kb

 

#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_;
		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() {
	}
	//----在此处添加关系运算符  >、<、>=、<=、==、!=  的重载原型声明
	bool operator> (MyCircle &a) {
		return this->radius_ > a.radius_;
	}

	bool operator< (MyCircle &a) {
		return this->radius_ < a.radius_;
	}

	bool operator== (MyCircle &a) {
		return this->radius_ == a.radius_;
	}

	bool operator>= (MyCircle &a) {
		return this->radius_ >= a.radius_;
	}

	bool operator<= (MyCircle &a) {
		return this->radius_ <= a.radius_;
	}

	bool operator != (MyCircle &a) {
		return this->radius_ != a.radius_;
	}

};
//----在此处添加关系运算符的重载定义
int main() {
	int r1, r2, r3 = 0;
	cin >> r1 >> r2 >> r3;
	MyCircle c1, c2, c3;
	c1.setRadius(r1);
	c2.setRadius(r2);
	c3.setRadius(r3);
	cout << (c1 > c2) << endl;
	cout << (c1 < c2) << endl;
	cout << (c2 >= c3) << endl;
	cout << (c2 <= c3) << endl;
	cout << (c1 == c3) << endl;
	cout << (c1 != c3) << endl;
	return 0;
}

 

  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值