Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Virtual and Override Keywords | Polymorphism Overview

Virtual and Override KeywordsVirtual 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.

h

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.

cpp

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.

cpp

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.

cpp

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.

1. What does the `virtual` keyword do when used before a method declaration in a class?
2. Which keyword is used to explicitly indicate that a member function overrides a virtual function?

What does the virtual keyword do when used before a method declaration in a class?

Select the correct answer

Which keyword is used to explicitly indicate that a member function overrides a virtual function?

Select the correct answer

Everything was clear?

Section 5. Chapter 2

Virtual and Override KeywordsVirtual 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.

h

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.

cpp

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.

cpp

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.

cpp

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.

1. What does the `virtual` keyword do when used before a method declaration in a class?
2. Which keyword is used to explicitly indicate that a member function overrides a virtual function?

What does the virtual keyword do when used before a method declaration in a class?

Select the correct answer

Which keyword is used to explicitly indicate that a member function overrides a virtual function?

Select the correct answer

Everything was clear?

Section 5. Chapter 2
some-alt