设计一个名为Rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getArea( )的函数返回矩形的面积;一个名为getPerimeter( )的函数返回矩形的周长。请实现这个类。编写一个测试程序,创建一个Rectangle对象,从键盘输入矩形的宽和高,然后输出矩形的面积和周长。
输入格式:
3.5 35.9(第一个数表示矩形的宽,第二个数表示矩形的高,中间是空间分隔。)
输出格式:
125.65 (第一行输出矩形的面积)
78.8 (第二行输出矩形的周长)
输入样例:
3.5 35.9
输出样例:
125.65
78.8
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include <iostream>
using namespace std;
class Rectangle
{
public:
Rectangle(double w = 1, double h = 1);
void setRectangle();
double getArea();
double getPerimeter();
private:
double width, height;
};
Rectangle::Rectangle(double w, double h)
{
width = w;
height = h;
}
void Rectangle::setRectangle()
{
cin >> width >> height;
}
double Rectangle::getArea()
{
return (width * height);
}
double Rectangle::getPerimeter()
{
return (2 * (width + height));
}
int main()
{
Rectangle r1;
r1.setRectangle();
cout << r1.getArea() << endl;
cout << r1.getPerimeter() << endl;
return 0;
}