【问题描述】声明一个Circle类,有数据成员Radius(半径)、成员函数GetArea(),并满足以下要求。 (1)需要定义和合理的构造函数。
(2)使用函数重载方式重载成员函数,当输入一个参数时,被认作创建圆,并认为输入圆半径,计算并返回圆的面积;当输入两个参数时,认为创建一个圆,计算并返回圆环的面积; (3)其他自行界定。
(4)编写合适的主函数,构造一个Circle的对象进行对象创建,以及成员函数的测试。
输入:
cin >> radius;
cin >> radius1 >> radius2;
重载:
GetArea(int radius) {
//计算圆面积
}
GetArea(int radius1, int radius2) {
//计算圆环面积
}
调用时可以传1个参数,也可以传2个参数
#define PI 3.14
class Circle
{
public:
Circle()
{
cout << "the circle is created" << endl;
}
void GetArea(int r1, int r2)
{
m_radiusExt = r1;
m_radiusInt = r2;
int r = m_radiusExt - m_radiusInt;
std::cout << "the ring's area is " << PI * r * r << endl;
}
void GetArea(int r1)
{
m_radiusExt = r1;
m_radiusInt = 0;
std::cout << "the area is " << PI * r1 * r1 << endl;
}
private:
int m_radiusInt = 0;
int m_radiusExt;
};
int main(int argc, char* argv[])
{
if (argc <= 1)
{
std::cout << argv[0] << std::endl;
std::cout << "Input error";
return -1;
}
for (int i = 0; i < argc; ++i)
{
std::cout << argv[i];
}
int value1(-1), value2(-1);
int count = argc -1;
if (1 == count)
{
value1 = std::atoi(argv[1]);
}
else if (2 == count)
{
value2 = std::atoi(argv[2]);
}
else
{
return -1;
}
Circle crcl;
if (1 == count)
{
crcl.GetArea(value1);
}
else if (2 == count)
{
crcl.GetArea(value1, value2);
}
return 0;
}