我试图使用我的基类“SHAPE”中的派生类“RECTANGLE”在我的类“BIGRECTANGLE”中创建一个更大的矩形的函数。我希望在课堂内部进行我的双方转变,而不是主要的,我该怎么做?谢谢!嵌套类和继承
#include <iostream>
using namespace std;
// Base class Shape
class Shape
{
public:
void ResizeW(int w)
{
width = w;
}
void ResizeH(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Primitive Shape
class Rectangle: public Shape
{
public:
int width = 2;
int height = 1;
int getArea()
{
return (width * height);
}
};
// Derived class
class BIGRectangle: public Rectangle
{
public:
int area;
Rectangle.ResizeW(8);
Rectangle.ResizeH(4);
area = Rectangle.getArea();
};
int main(void)
{
return 0;
}
这些是我的错误: - 45:14:错误:之前预期不合格的ID ''令牌 - 46:14:错误:预计在''之前的非限定ID。'代币 - 47:5:错误:'区域'没有指定类型
将这些东西放在构造函数中或其他什么...你知道什么是构造函数吗?你没有使用它们。 – LogicStuff
@LogicStuff你能帮我弄清楚吗? – FL93
这里是关于构造函数的[tutorial](http://www.cplusplus.com/doc/tutorial/classes/)的链接。还有[继承](https:// www。cs.bu.edu/teaching/cpp/inheritance/intro/)。阅读它们; Google是你的朋友。 – NonCreature0714