`override` — C++ Keyword
`override` — C++ Keyword
The override keyword in C++: marks a virtual function as intentionally overriding a base class virtual.
`override` — C++ Keyword
The override keyword in C++: marks a virtual function as intentionally overriding a base class virtual.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
overrideA context-sensitive keyword that explicitly marks a virtual member function as overriding a base-class virtual. The compiler verifies the override is valid and issues an error if no matching virtual exists.
struct Derived : Base {
return-type function-name(params) override;
};
#include <print>
struct Animal {
virtual std::string sound() const { return "..."; }
virtual ~Animal() = default;
};
struct Dog : Animal {
std::string sound() const override { return "Woof"; }
};
struct Cat : Animal {
std::string sound() const override { return "Meow"; }
};
// Uncommenting the line below would be a compile error:
// std::string soudn() const override { return "?"; } // typo – no base virtual
int main() {
Dog d;
Cat c;
std::println("{}", d.sound()); // Woof
std::println("{}", c.sound()); // Meow
}
override catches signature mismatches (wrong const, wrong parameter types) that would silently create a new non-virtual function instead of overriding.override when overriding virtual functions.final to prevent further overriding: void func() override final;overrideint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}