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 subclass but require distinct implementations for each. Essentially, it looks like any other method declaration, except it begins with virtual
.
Base.h
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.cpp
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.cpp
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.cpp
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.
Everything was clear?
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 subclass but require distinct implementations for each. Essentially, it looks like any other method declaration, except it begins with virtual
.
Base.h
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.cpp
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.cpp
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.cpp
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.
Everything was clear?