class RECT{ protected: int x,y; public: RECT(int x1, int y1) { x=x1,y=y1; }; virtual int area() { return x*y; }; int peri() { return 2*x+2*y; }; virtual int isSquare() { if(x==y) return 1; else return 0; }; }; class CUB:public RECT{ private: int height; public: CUB(int x,int y,int h):RECT(x,y) { height=h; }; int volume() { int volume,ar; ar=area(); volume=ar*height; return volume; }; int area() { int ar1,c1; ar1=area(); c1=peri(); return 2*ar1+c1*height; }; int isSquare() { if(isSquare()==1&&height==x) return 1; else return 0; } };
时间: 2023-12-02 11:02:36 浏览: 26
这段代码定义了两个类,一个是矩形类 RECT,一个是立方体类 CUB,CUB 是 RECT 的子类。RECT 类有两个私有成员 x 和 y,一个构造函数和三个成员函数:area()、peri() 和 isSquare(),其中 area() 和 isSquare() 是虚函数。CUB 类有一个私有成员 height,一个构造函数和三个成员函数:volume()、area() 和 isSquare(),其中 area() 和 isSquare() 是重载了 RECT 类中的虚函数。volume() 函数用于计算立方体的体积,area() 函数用于计算立方体的表面积,isSquare() 函数用于判断立方体是否为正方体。
相关问题
QRect(int x, int y, int width, int height)
QRect是Qt框架中定义的一个类,用于表示一个矩形区域。它有多个构造函数,其中一个常用的是QRect(int x, int y, int width, int height),该构造函数用于创建一个左上角坐标为(x,y),宽为width,高为height的矩形区域。例如,以下代码创建了一个左上角坐标为(0,0),宽为100,高为50的矩形区域:
```cpp
QRect rect(0, 0, 100, 50);
```
除此之外,QRect还提供了很多其他的成员函数,用于获取或修改矩形区域的各个属性,比如left()、right()、top()、bottom()等函数用于获取矩形的左、右、上、下边界坐标,setWidth()、setHeight()等函数用于修改矩形的宽、高等。
class Rect { public: Rect() // 构造函数,计数器加1 { count++; } ~Rect() // 析构函数,计数器减1 { count--; } static int getCount() // 返回计数器的值 { return count; } private: int width; int height; static int count; // 一静态成员做为计数器 }; int Rect::count = 0; // 初始化计数器
这段代码定义了一个名为 `Rect` 的类,其中包括一个静态成员变量 `count`,用于记录创建了多少个 `Rect` 对象。当创建一个新的 `Rect` 对象时,构造函数会将 `count` 自增1,而当 `Rect` 对象被销毁时,析构函数会将 `count` 自减1。通过静态成员函数 `getCount()` 可以获取当前 `count` 的值。最后一行代码 `int Rect::count = 0;` 是对静态成员变量 `count` 进行初始化,将其初始值设为0。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)