我試圖使用我的基類「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