【发布时间】:2020-04-26 05:28:13
【问题描述】:
我的问题实际上是两个方面。我正在阅读本教程https://www.youtube.com/watch?v=6y0bp-mnYU0,在 1:07:45,他谈到了在定义抽象类的虚拟函数时使用 override 关键字。我将类声明和定义保存在不同的文件中。 当我尝试在我的定义中使用 override 时,它在 Visual Studio 2019 上的函数定义中给了我“'override' 说明符非法”。这是为什么?
包括“Circle.h” 包括“Shape.h”
double Circle::Area() override {
return 3.14159 * pow((width / 2), 2);
}
另外,这段代码 sn-p 有什么作用?我是 C++ 新手:
Circle::Circle(double width) : Shape(width) {
}
为什么 circle 使用抽象类的构造函数?这甚至可能吗? : Shape(width) 有什么作用。
这是“Shape”类的样子:
class Shape
{
protected: //means that inherited classes will be able to access as long as it is part of protected
double height;
double width;
public:
static int numofShapes;
Shape(double length);
Shape(double height, double width);
Shape();
Shape(const Shape& orig);
virtual ~Shape();
//Setters and getters for privat mems
void Setheight(double height);
double Getheight();
void Setwidth(double height);
double Getwidth();
static int Getnumofshapes();
virtual double Area() = 0; //makes it an abstract base class
//私有:只有类代码
【问题讨论】:
-
如果由于定义在声明之外而不需要覆盖,编译器如何知道正确覆盖?
-
Shape不是抽象的(它没有任何纯虚函数),它也没有virtual double Area()函数你可以override
标签: c++