[英]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
.在您的代码中Triangle
和Rectangle
继承自Polygon
,因此它们也继承了Polygon
的成员height
和width
。 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
继承的成员之外, Triangle
和Rectangle
还具有成员height
和width
。 In Triangle
and Rectangle
s methods you are using those members.在Triangle
和Rectangle
的方法中,您正在使用这些成员。 They are distinct from the members inherited from Polygon
.它们与继承自Polygon
的成员不同。 Members of Triangle
and Rectangle
you do not initialize.您不初始化的Triangle
和Rectangle
的成员。
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;
}
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.