In this code:
class Base {
public:
virtual void method() = 0;
};
class Derived1 : public Base{
public:
virtual void method() override { }
};
class Derived2 : public Base{
public:
void method() override { }
};
Is there any difference between Derived1
and Derived2
?
A virtual method defined in the base class can be overridden in a derived class by defining a method with the same signature and marking it with the override keyword.
Base: Non-virtual display. because the Display method of the Base class is not a virtual method so the Derived class should not be able to override it.
From section 10.3 Virtual functions of the c++11 standard (draft n3337) point 2:
If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and refqualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.
So Derived2::method
is also virtual
, even though it is not explicitly declared as such.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With