简体   繁体   English

从 CPP 中的抽象 class 继承

[英]Inheriting from an abstract class in CPP

I've been learning about CPP inheritance and I cam across this question:我一直在学习 CPP inheritance 并且遇到了这个问题:

   Class polygon contains data member width and height and public method set_value() to assign values to width and height.
 Class Rectangle and Triangle are inherited from polygon class. 
the classes contain public method calculate_area() to calculate the area of Rectangle and Triangle.
 Use base class pointer to access the derived class object and show the area calculated. 

I wrote the following program for this problem:我为这个问题编写了以下程序:

#include <iostream>
using namespace std;
class Polygon{
    float height,width;
    public:
    void set_value(float height, float width){
        this->height=height;
        this->width=width;
    }
    virtual void calculate_area()=0;
};
class Rectangle:public Polygon{
    float height,width;
    public:
    void calculate_area(){
        cout << "Area of the rectangle is " << height*width <<endl;
    }
};
class Triangle:public Polygon{
    float height,width;
    public:
    void calculate_area(){
        cout << "Area of the triangle is " << 0.5*height * width << endl;
    }
};
int main(){
    Rectangle r1;
    r1.set_value(10,20);
    Triangle t1;
    t1.set_value(10,20);
    Polygon *p1;
    Polygon *p2;
    p1=&r1;
    p2=&t1;
    p1->calculate_area();
    p2->calculate_area();
    return 0;
}

However, on executing, it shows area of rectangle and triangle as zero.但是,在执行时,它将矩形和三角形的面积显示为零。 On further debugging, I found out that all dimensions (ie height and width of rectangle and triangle have value as zero).在进一步调试中,我发现所有尺寸(即矩形和三角形的高度和宽度的值为零)。 Can someone tell what's wrong with the code?有人可以告诉代码有什么问题吗?

In your code Triangle and Rectangle inherit from Polygon , hence they also inherit Polygon s members height and width .在您的代码中TriangleRectangle继承自Polygon ,因此它们也继承了Polygon的成员heightwidth Its those members that are used in Polygon s methods.它是在Polygon的方法中使用的那些成员。 Though they are private , hence cannot be accessed outside of Polygon .尽管它们是private的,因此不能在Polygon之外访问。

Triangle and Rectangle have members height and width in addition to those inherited from Polygon .除了从Polygon继承的成员之外, TriangleRectangle还具有成员heightwidth In Triangle and Rectangle s methods you are using those members.TriangleRectangle的方法中,您正在使用这些成员。 They are distinct from the members inherited from Polygon .它们与继承自Polygon的成员不同。 Members of Triangle and Rectangle you do not initialize.您不初始化的TriangleRectangle的成员。

TL;DR: One possible fix is to grant derived classes access to the members by declaring them protected and remove the superflous definition of members in the derived classes: TL;DR:一种可能的解决方法是通过声明它们为protected的成员并删除派生类中成员的多余定义来授予派生类对成员的访问权限:

#include <iostream>
using namespace std;
class Polygon{
    protected:
    float height,width;
    public:
    void set_value(float height, float width){
        this->height=height;
        this->width=width;
    }
    virtual void calculate_area()=0;
};
class Rectangle:public Polygon{
    public:
    void calculate_area() override {
        cout << "Area of the rectangle is " << height*width <<endl;
    }
};
class Triangle:public Polygon{    
    public:
    void calculate_area() override {
        cout << "Area of the triangle is " << 0.5*height * width << endl;
    }
};
int main(){
    Rectangle r1;
    r1.set_value(10,20);
    Triangle t1;
    t1.set_value(10,20);
    Polygon *p1;
    Polygon *p2;
    p1=&r1;
    p2=&t1;
    p1->calculate_area();
    p2->calculate_area();
    return 0;
}

Output : Output

Area of the rectangle is 200
Area of the triangle is 100

Note that I added override to the implementations of the virtual methods.请注意,我在虚拟方法的实现中添加了override When the overrides are ok, then it has not impact, but it will trigger a compiler error in case the method marked as override does actually not override (something that can happen easily by typos).当覆盖正常时,它没有影响,但如果标记为override的方法实际上没有覆盖(错字很容易发生这种情况),它将触发编译器错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从模板抽象类继承 - Inheriting from template abstract class 从C ++中的Abstract类继承 - Inheriting from Abstract Class in C++ 从抽象类继承的模板类中正确使用未定义类型 - Proper use of undefined type in template class inheriting from abstract class 在继承自抽象 class 的 class 中使用子类 object 作为参数 - Using a subclass object as a parameter in a class inheriting from an abstract class 当从抽象类继承以创建另一个抽象类时,我应该重新声明所有虚函数吗? - When inheriting from an abstract class to create another abstract class should I redeclare all virtual functions? 有什么方法可以实现抽象类,它是从 C++ 中的其他抽象类多重继承的? - Are there any way to make implementation for abstract class which is multiple inheriting from other abstract classes in c++? Diamond继承-从抽象类以及具体类继承而不是实现基于共享的类 - Diamond inheritence - Inheriting from an abstract class and also a concrete class than implements a shared based class 抽象类继承具有相同功能名称的另一个抽象类 - Abstract class inheriting another abstract class with the same function name 抽象类继承了C ++中的另一个抽象类 - Abstract class inheriting another abstract class in C++ 虚拟化继承抽象类的参数化构造函数 - Parametrized constructor in virtually inheriting abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM