【问题标题】:inheritance Classes [closed]继承类[关闭]
【发布时间】:2016-06-02 18:08:54
【问题描述】:

第一部分

我正在尝试使用基类“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: 错误: '.' 之前的预期 unqualified-id令牌 - 46:14: 错误: '.' 之前的预期 unqualified-id令牌 - 47:5: 错误:“区域”没有命名类型

【问题讨论】:

  • 1. .using std namespace...。 2.将public Rectangle, public Shape更改为public Rectangle - 矩形已经是一个形状
  • 您删除了代码,使我的评论没有任何意义?
  • @EdHeal 但这仍然会给我留下:45:10:错误:'.'之前的预期不合格ID令牌 46:10:错误:“。”之前的预期不合格 ID令牌 47:1:错误:“区域”未命名类型
  • @EdHeal 我正在编辑我的代码
  • 请留下代码。也许试试我的改变,告诉我你是怎么过的

标签: c++ class geometry coordinates void


【解决方案1】:

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

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

#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;
}

【讨论】:

  • 完美的不明确!
猜你喜欢
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2014-03-13
  • 2017-01-12
  • 2017-01-19
  • 2013-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多