Contenido del Curso
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 subclass but require distinct implementations for each. Essentially, it looks like any other method declaration, except it begins with virtual
.
Base
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 subclass. This means it can provide its own implementation of the function, which will be invoked instead of the original implementation when the function is called through a pointer or reference to the superclass.
main
#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(); }
Note
When you have a pointer to an object and you want to access members of it through the pointer, you use the arrow operator (
->
).
Returning to the solution involving original problem with buttons, this method enables us to easily create a single function that take a parent Button pointer as a parameter. Consequently, we can invoke this function by passing either SubmitButton, ResetButton, or CancelButton, and it will function as intended.
main
#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); }
Note
Try to create objects of different subclass and pass them to the function.
The Override Keyword
The override
keyword is used in 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
#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 particularly beneficial in large team environments, where tracking for example change of the method names can be challenging.
¡Gracias por tus comentarios!