题目
设计一个名为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
答案
#include<iostream>
using namespace std;
class Rectangle{
private:
double width = 1;
double height = 1;
public:
Rectangle(){}
Rectangle(double width,double height) {
this->width = width;
this->height = height;
}
double getArea() {
return width * height;
}
double getPerimeter() {
return width * 2 + height * 2;
}
};
int main() {
double width,height,area,perimeter;
cin >> width >> height;
Rectangle rectangle(width, height);
area = rectangle.getArea();
perimeter = rectangle.getPerimeter();
cout << area << endl << perimeter << endl;
return 0;
}