# include <iostream>
using namespace std;
# include <iomanip>
# include <math.h>
class Rectangle
{
double area;
public:
Rectangle()
{
double area = 0;
}
Rectangle(double a);
Rectangle(double b, double c);
};
int main()
{
double a, b, c;
cout << "计算正方形面积(输入边长)" << endl;
cin >> a;
Rectangle(a);
cout << "计算长方形面积(输入长和宽)" << endl;
cin >> b >> c;
Rectangle(b, c);
return 0;
}
Rectangle::Rectangle(double x)
{
double area = x * x;
cout << "正方形面积为:" << endl;
cout << area << endl;
}
Rectangle::Rectangle(double b, double c)
{
double area = c * b;
cout << "长方形面积为:" << endl;
cout << area << endl;
}

函数重载问题,为什么a会重定义
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- aabyte 2021-10-07 17:23关注
修改如下:
# include <iostream> using namespace std; # include <iomanip> # include <math.h> class Rectangle { double area; public: Rectangle() { double area = 0; } Rectangle(double a); Rectangle(double b, double c); }; int main() { double a, b, c; cout << "计算正方形面积(输入边长)" << endl; cin >> a; Rectangle T(a); //要实例化 cout << "计算长方形面积(输入长和宽)" << endl; cin >> b >> c; Rectangle H(b, c); //要实例化 return 0; } Rectangle::Rectangle(double x) { double area = x * x; cout << "正方形面积为:" << endl; cout << area << endl; } Rectangle::Rectangle(double b, double c) { double area = c * b; cout << "长方形面积为:" << endl; cout << area << endl; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报