编写程序在circle类中实现关系运算符(大于,小于,等于),实现按照半径circle对象排序。 自己定义类中所需的成品。实现上述功能

#include<iostream>
#include<cmath>
#include<iomanip>
#define pi 3.1415926
using namespace std;
class circle
{
public:
	circle() { radius = 0; }
	circle(double);
	double area();
	friend bool operator >(circle&, circle&);
	friend bool operator <(circle&, circle&);
	friend bool operator ==(circle&, circle&);
	friend istream& operator>>(istream&, circle&);
	friend ostream& operator<<(ostream&, circle&);
private:
	double radius;
};
circle::circle(double r)
{
	radius = r;
}
double circle::area()
{
	return pi * pow(radius, 2);
}
bool operator >(circle& c1, circle& c2)
{
	if (c1.radius > c2.radius)
		return true;
	else
		return false;
}
bool operator <(circle& c1, circle& c2)
{
	if (c1.radius < c2.radius)
		return true;
	else
		return false;
}
bool operator ==(circle& c1, circle& c2)
{
	if (c1.radius == c2.radius)
		return true;
	else
		return false;
}
istream& operator>>(istream& input, circle& c)
{
	static int i = 0;
	cout << "please enter the radius of c" << ++i << ":" << endl;
	input >> c.radius;
	return input;
}
ostream& operator <<(ostream & output, circle & c)
{
	output << c.radius;
	return output;
}
void sorting(circle&c1,circle&c2)
{
	cout << "the two circles are arranged in order from large to small:" << endl;
	if (operator>(c1, c2) == 1)
		cout << c1 << '\t' << c2 << endl;
	else if (operator<(c1, c2) == 1)
		cout << c2 << '\t' << c1 << endl;
	else if (operator==(c1, c2) == 1)
		cout << c1 << "=" << c2 << endl;
}
int main()
{
	circle c1, c2;
	cin >> c1 >> c2;
	sorting(c1, c2);
	cout << "the area of c1 is:" << setiosflags(ios::fixed) << setprecision(2) << c1.area() << endl;
	cout << "the area of c2 is:" << setiosflags(ios::fixed) << setprecision(2) << c2.area() << endl;
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值