#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;
}
编写程序在circle类中实现关系运算符(大于,小于,等于),实现按照半径circle对象排序。 自己定义类中所需的成品。实现上述功能
于 2022-05-02 19:47:56 首次发布