Course Content
C++ OOP
C++ OOP
Virtual and Override Keywords
Virtual Keyword
The virtual
keyword is used before a method declaration in a class
to solve the issue we discussed earlier. It's employed for methods that are common among all subclasses but require distinct implementations for each. Essentially, it looks like any other method declaration, except it begins with virtual
.
Base.h
class Base { public: virtual void display() { std::cout << "Base class display() called" << std::endl; } };
When a function is declared as virtual
in a superclass, it allows it to be overridden in a subclass. This means the subclass can provide its own implementation, which will be invoked instead of the original implementation when the function is called through a pointer or reference to the superclass.
main.cpp
#include <iostream> class Base { public: virtual void display() { std::cout << "Base display() called" << std::endl; } }; class Derived : public Base { public: void display() { std::cout << "Derived display() called" << std::endl; } }; int main() { Base* obj; Derived derived; obj = &derived; obj->display(); }
When you have a pointer to an object and you want to access its members through the pointer, you use the arrow operator (->
).
Returning to the solution for the original problem with buttons, this method enables us to easily create a single function that takes a parent Button
pointer as a parameter. Consequently, we can invoke this function by passing a SubmitButton
, ResetButton
, or CancelButton
, and it will behave as intended.
main.cpp
#include <iostream> class Button { public: virtual void onClick() { std::cout << "Click" << std::endl; } }; class UploadButton : public Button { public: void onClick() { std::cout << "Upload" << std::endl; } }; class ResetdButton : public Button { public: void onClick() { std::cout << "Reset" << std::endl; } }; class CancelButton : public Button { public: void onClick() { std::cout << "Cancel" << std::endl; } }; void user_clicked_button(Button& btn) { btn.onClick(); } int main() { UploadButton uploadButton; user_clicked_button(uploadButton); }
Try to create objects of different subclasses and pass them to the function.
The Override Keyword
The override
keyword is used in a subclass to explicitly indicate that a member function overrides a virtual function from a superclass. It helps catch errors at compile time if the function signature in the subclass does not match any virtual function in the superclass.
main.cpp
#include <iostream> class Button { public: virtual void handleClick() { std::cout << "Click" << std::endl; } }; class UploadButton : public Button { public: // marked override, but does not override void onClick() override { std::cout << "Upload" << std::endl; } }; int main() { }
If there's a mistake, such as a typo in the function signature or the superclass does not have the function you're attempting to override, the compiler will generate an error, helping to catch potential bugs at compile time. This feature is especially useful in large team environments, where tracking changes like method renaming can be challenging.
Thanks for your feedback!