作业1

题目:

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
35
36
37
38
39
40
#include<iostream>
using namespace std;

class Rectangle {
private:
float width, height;
public:
Rectangle();//无参数构造函数
Rectangle(float width, float height);//声明构造函数
void Setwidth(float width) {//设置新的宽度
this ->width = width;
}
void Setheight(float height) {//设置新的高度
this->height = height;
}
float getArea(){
return height * width;
}
float getPerimeter() {
return (height + width) * 2;
}
};
Rectangle::Rectangle() {//无参数构造函数使用
this->width = 1.0;
this->height = 1.0;
}
Rectangle::Rectangle(float width, float height) {//有参数构造函数使用
this->width = width;
this->height = height;
}
int main(void) {
Rectangle rec_1;//
cout << "无参构造函数的面积为:" << rec_1.getArea() <<" 长度为:" <<rec_1.getPerimeter()<< endl;
Rectangle rec_2(4,5);
cout << "有参构造函数的面积为:" << rec_2.getArea() << " 长度为:" << rec_2.getPerimeter() << endl;
rec_2.Setheight(8);//长度修改为8
cout << "修改后的有参构造函数的面积为:" << rec_2.getArea() << " 长度为:" << rec_2.getPerimeter() << endl;
system("pause");
return 0;
}