6-1 定义一个矩形类(C++构造函数) (10 分)

设计一个名为Rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getArea( )的函数返回矩形的面积;一个名为getPerimeter( )的函数返回矩形的周长。请实现这个类。

类名为:
Rectangle
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include
using namespace std;
//你提交的代码将嵌入到这里

int main()
{
double m,n;
cin>>m;
cin>>n;
Rectangle a(m,n);
cout<<a.getArea()<<endl;
cout<<a.getPerimeter()<<endl;
return 0;
}
输入样例:
3.5 35.9
输出样例:
125.65
78.8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Rectangle
{
private:
double width;//宽
double height;//高
public:
Rectangle()//无参构造函数
{
width=1;
height=1;
}
Rectangle(double x,double y)//有参构造函数
{
width=x;
height=y;
}
double getArea()//得到面积
{
double area;
area=width*height;
//cout<<"面积为:"<<area<<endl;
return area;
}
double getPerimeter()//得到周长
{
double perimeter;
perimeter=2*(width+height);
//cout<<"周长为:"<<perimeter<<endl;
return perimeter;
}

};



6-1 定义一个矩形类(C++构造函数) (10 分)
https://6jackjiang.github.io/2022/01/25/categories/c++/6-1 定义一个矩形类(C++构造函数) (10 分)/
作者
米兰的小铁匠
发布于
2022年1月25日
许可协议