C++ 为什么不允许继承成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15117591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:03:36  来源:igfitidea点击:

Why is inherited member not allowed?

c++inheritance

提问by user1447343

I'm beginner to C++ and I'm doing one of the exercises about abstract class and inheritance.

我是 C++ 的初学者,我正在做关于抽象类和继承的练习之一。

This is my abstract class:

这是我的抽象类:

#ifndef   SHAPE_H 
#define  SHAPE_H
class Shape
{
    public:
        virtual void area();
        virtual void perimeter();
        virtual void volume();
};
#endif

This is my concrete class that implements the abstract class:

这是我实现抽象类的具体类:

#include <iostream>
#include <cmath>
#include "Shape.h"
using namespace std;

class Circle : public Shape
{
    public:
        Circle(int);
    private:
        int r;
};

Circle::Circle(int rad)
{
    r = rad;
}

void Circle::area()
{
    cout << "Area of this cirle = " << 3.14 * pow(r, 2) << endl;
}

void Circle::perimeter()
{
    cout << "Perimeter of this cirle = " << 2 * 3.14 * r << endl;
}

void Circle::volume()
{
    cout << "Volume is not defined for circle." << endl;
}

I got red lines under area(), perimeter(), and volume()in my Circleclass, which showed "Error: inherited member is not allowed". I went through my class ppt and googled for answer but no luck. Any help is appreciated.

我在area(),perimeter()volume()在我的Circle班级下有红线,显示"Error: inherited member is not allowed"。我浏览了我的课堂ppt并用谷歌搜索答案,但没有运气。任何帮助表示赞赏。

回答by simonc

You have to declare the over-ridden functions as part of your class definition

您必须将重写的函数声明为类定义的一部分

class Circle : public Shape
    {
    public:
        Circle(int);
        virtual void area(); // overrides Shape::area
        void perimeter();    // overrides Shape::perimeter
        virtual void volume();
    private:
        int r;
    };

Note that the use of virtualhere is optional.

请注意,virtual此处的使用是可选的。

As n.m. noted, you should also include a virtual destructor in Shape. You may also want to make its virtual functions pure virtual (based on your comment about Shapebeing abstract)

正如 nm 所指出的,您还应该在Shape. 您可能还想使其虚函数纯虚(基于您对Shape抽象的评论)

class Shape
{
public:
    virtual ~Shape() {}
    virtual void area() = 0;
    virtual void perimeter() = 0;
    virtual void volume() = 0;
};

回答by V-X

you have to declare the override methods too in the Circle class

您也必须在 Circle 类中声明覆盖方法

class Circle : public Shape
    {
    public:
        Circle(int);
        virtual void area();
        virtual void perimeter();
        virtual void volume();
    private:
        int r;
    };

回答by Slava

First you should make you Shape class explicitly abstract:

首先,您应该使 Shape 类显式抽象:

class Shape
{
public:
    virtual void area() = 0;
    virtual void perimeter() = 0;
    virtual void volume() = 0;
};

This way you do not have to define that methods in class Shape, and what is more important if you forget to override any of abstract method in derived class and would try to create an instance of it, compiler will remind you. Second when you override virtual method in derived class you need to declare them:

这样您就不必在类 Shape 中定义该方法,更重要的是如果您忘记覆盖派生类中的任何抽象方法并尝试创建它的实例,编译器会提醒您。其次,当您覆盖派生类中的虚方法时,您需要声明它们:

class Circle : public Shape
{
public:
    Circle(int);

    virtual void area();
    virtual void perimeter();
    virtual void volume();
private:
    int r;
};