继承 类

inheritance Classes

A 部分

我正在尝试使用我的基础 class "SHAPE" 中的函数和派生的 class "RECTANGLE" 在我的 class "BIGRECTANGLE"。我想在 class 内而不是在主体内进行两侧变换,我该怎么办?谢谢!

#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: 错误: 'area' 没有命名类型

这不是答案 - 所以我很抱歉。

我不能在评论中这样做 - 所以请原谅我

#include <iostream>

using namespace std; // This is a bad idea

// Base class Shape
class Shape // THIS IS THE BASE CLASS - It has height as a member 
{
public:

    void ResizeW(int w)
    {
        width = w;
    }
    void ResizeH(int h)
    {
        height = h;
    }
protected:

    int width;
    int height;
};

// Primitive Shape

class Rectangle: public Shape // This is derived class, it inherits height
{
public:

    int width = 2;
    int height = 1; // And here it is!

    int getArea()
    {
        return (width * height);
    }
};

// Derived class

class BIGRectangle: public Rectangle
{
public:

    int area;
    Rectangle.ResizeW(8);
    Rectangle.ResizeH(4);
    area = Rectangle.getArea(); // This is not valid C++ code
};

int main(void)
{
    return 0;
}