Below you will see 3 classes. My question is why can I use override
on the getArea()
of the FunnySquare
Class, even though it is not tagged as virtual in the Square
Class.
My assumption is that an overridden pure virtual function is virtual even though it is not specified, but I am not sure that is true and could not find any references to confirm.
class Shape {
public:
virtual float getArea() = 0;
};
class Square : public Shape {
private:
float length;
public:
Square(float len){
length = len;
}
float getArea() override {
return length * length;
}
void sayHi() {cout << "hell0" << endl;}
};
class FunnySquare : public Square {
public:
FunnySquare(float len) : Square(len) {}
void tellJoke() {std::cout << "I gave you the wrong area haha" << endl;}
float getArea() override {
return 2.0;
}
};
Yes, both Square::getArea
and FunnySquare::getArea
are virtual
too.
(emphasis mine)
Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration).