编写一个Rect类,要求: (1)Rect类的私有成员变最有: a: int型,代表长。b:int型,代表宽。 (2)Rect类的公有成员方法有: Rect():构造方法,默认的a、b的值都是0。 Rectint len, int width)分别用两个形式参数len、 width对成员变最a、b进行初始化, double area()计算矩形的面积 void display():输出矩形的长、宽和面积。 (3)然后编写一个含有main方法的类UseRect,创建Rect类的对象,并调用上面定义的方法。
时间: 2023-05-22 19:06:13 浏览: 71
Rect类的代码如下:
```cpp
class Rect {
private:
int a, b;
public:
Rect(): a(0), b(0) {}
Rect(int len, int width): a(len), b(width) {}
double area() { return a * b; }
void display() {
cout<<"长:"<<a<<endl;
cout<<"宽:"<<b<<endl;
cout<<"面积:"<<area()<<endl;
}
};
```
UseRect类的代码如下:
```cpp
int main() {
Rect r1; // 默认构造函数
Rect r2(5, 6); // 传递两个参数
r1.display();
r2.display();
return 0;
}
```
运行结果为:
```
长:0
宽:0
面积:0
长:5
宽:6
面积:30
```
相关问题
C++编写一个表示矩形的类Rect, 包含两个私有成员w,h分别表示举行的宽和高。编写合适的构造函数、计算面积的函数以及访问和修改成员变量的函数。 从Rect 派生出一个表示正方形的类Square, 编写合适的构造函数、改写(override)修改成员变量的函数。注意:正方形的宽和高相等。
好的,我会为您解答关于C++的问题。
首先,我们可以编写一个表示矩形的类Rect,代码如下:
```c++
class Rect {
private:
int w, h; // 矩形的宽和高
public:
Rect(int width, int height): w(width), h(height) {} // 构造函数
int getArea() const { return w * h; } // 计算面积的函数
int getWidth() const { return w; } // 获取宽度的函数
int getHeight() const { return h; } // 获取高度的函数
void setWidth(int width) { w = width; } // 设置宽度的函数
void setHeight(int height) { h = height; } // 设置高度的函数
};
```
接下来,我们可以从Rect派生出一个表示正方形的类Square,代码如下:
```c++
class Square : public Rect {
public:
Square(int side) : Rect(side, side) {} // 构造函数
void setWidth(int side) override { Rect::setWidth(side); Rect::setHeight(side); } // 修改宽度的函数
void setHeight(int side) override { Rect::setWidth(side); Rect::setHeight(side); } // 修改高度的函数
};
```
在这个类中,我们使用了`Rect::setWidth(side)`和`Rect::setHeight(side)`来修改父类的私有成员变量。注意,我们使用了`override`关键字来指示这两个函数覆盖了父类的函数。
希望这个回答对您有所帮助!
设计 Rectangle 类和实现两个普通函数,类中有两个成员变量和两个函数,其中成员变量的访问性为私有,函数的访问性为公有,具体要求如下: 高度:int height 宽度:int width 设置长方形的高和宽函数:void Set(int h, int w) 获取长方形的面积函数:int GetArea() Rectangle GetRect(int h,int w)普通函数,h 、w 分别代表长方形的高宽,函数用来创建一个 Rectangle 对象并返回。 int GetRectArea(Rectangle rect)普通函数,函数用来获取 rect 对象的面积。
class Rectangle {
private:
int height;
int width;
public:
void Set(int h, int w) {
height = h;
width = w;
}
int GetArea() {
return height * width;
}
};
Rectangle GetRect(int h, int w) {
Rectangle rect;
rect.Set(h, w);
return rect;
}
int GetRectArea(Rectangle rect) {
return rect.GetArea();
}
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)