继承类 [英] inheritance Classes

查看:164
本文介绍了继承类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A部分

我想使用我在基类SHAPE中使用派生类RECTANGLE的函数创建一个更大的矩形在我的类BIGRECTANGLE。我想做我的双方转换里面的类,而不是在主,我应该怎么办?谢谢!

I'm trying to use the function that I have inside my base class "SHAPE" with the derived class "RECTANGLE" to create a bigger rectangle in my class "BIGRECTANGLE". I want to do have my sides transformation inside the class and not in the main, What should I do? Thanks!

#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'类型

These are the errors that I have: - 45:14: error: expected unqualified-id before '.' token - 46:14: error: expected unqualified-id before '.' token - 47:5: error: 'area' does not name a type

推荐答案

这不是一个答案 - 非常抱歉。

This is not an answer - so I apologies.

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

I cannot this of doing this in a comment - so forgive me

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

这篇关于继承类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆