设计 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 对象的面积。
时间: 2023-05-29 20:04:10 浏览: 129
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();
}
相关问题
创建一个Rectangle类,添加width和height两个私有成员变量,并提供getter及setter;
好的,这是一个关于类的编程问题,我可以为您解答。以下是创建一个Rectangle类,添加width和height两个私有成员变量,并提供getter及setter的示例代码:
```
class Rectangle {
private:
int width;
int height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
int getWidth() {
return width;
}
void setWidth(int w) {
width = w;
}
int getHeight() {
return height;
}
void setHeight(int h) {
height = h;
}
};
```
通过以上代码,您可以创建一个Rectangle类,并在类中添加width和height两个私有成员变量。通过提供getWidth、getHeight、setWidth、setHeight四个函数,您可以实现对这两个私有成员变量的访问和修改。当然,您也可以选择使用C++11中新增的getter和setter特性,更加方便地实现对成员变量的访问和修改。
使用c#定义长方形的类,并为其成员函数计算两个给定的长方形的周长和面积
好的,以下是使用 C# 定义长方形类并计算周长和面积的完整程序:
```c#
using System;
namespace ConsoleApp1
{
class Rectangle
{
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public double GetPerimeter()
{
return 2 * (length + width);
}
}
class Program
{
static void Main(string[] args)
{
Rectangle rect1 = new Rectangle(4, 5);
Rectangle rect2 = new Rectangle(6, 7);
Console.WriteLine("第一个长方形的面积为:" + rect1.GetArea() + ",周长为:" + rect1.GetPerimeter());
Console.WriteLine("第二个长方形的面积为:" + rect2.GetArea() + ",周长为:" + rect2.GetPerimeter());
Console.ReadKey();
}
}
}
```
这个程序定义了一个 `Rectangle` 类,它有两个私有成员变量 `length` 和 `width`,表示长方形的长度和宽度。它有一个构造函数,用于初始化 `length` 和 `width`。它还有两个公共成员函数 `GetArea()` 和 `GetPerimeter()`,用于计算长方形的面积和周长。
在 `Main()` 函数中,我们创建了两个 `Rectangle` 对象 `rect1` 和 `rect2`,并调用它们的 `GetArea()` 和 `GetPerimeter()` 函数来计算它们的面积和周长,并输出结果。
希望这个程序能够帮助到您!
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)