修改一下的代码错误:#include "iostream.h>" class Rectangle //矩形类 { private: int length; //矩形的长 protected: int width; //矩形的宽 public: Rectangle(int l = 10, int w = 10):length(l), width(w) { } int calArea() { return length * width; } }; class Cuboid : public Rectangle //长方体类,从Rectangle公有派生 { private: int height; //长方体的高 public: Cuboid(int l = 10, int w = 10, int h = 10) { length = l; width = w; height = h; } int calVolume() { return calArea() * height; } }; void main(void) { Cuboid test(10, 20, 30); cout << test.calVolume() << endl; }
时间: 2023-12-10 09:40:05 浏览: 73
修改后的代码如下:
```
#include <iostream>
using namespace std;
class Rectangle //矩形类
{
private:
int length; //矩形的长
protected:
int width; //矩形的宽
public:
Rectangle(int l = 10, int w = 10):length(l), width(w)
{
}
int calArea()
{
return length * width;
}
};
class Cuboid : public Rectangle //长方体类,从Rectangle公有派生
{
private:
int height; //长方体的高
public:
Cuboid(int l = 10, int w = 10, int h = 10): Rectangle(l, w), height(h)
{
}
int calVolume()
{
return calArea() * height;
}
};
int main(void)
{
Cuboid test(10, 20, 30);
cout << test.calVolume() << endl;
return 0;
}
```
修改的部分:
1. iostream头文件应该是#include <iostream>
2. main函数的返回类型应该是int而不是void
3. Cuboid类的构造函数应该调用基类Rectangle的构造函数来初始化基类成员变量
4. main函数中应该有return 0;语句
阅读全文