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
C++ OOP
course content

Зміст курсу

C++ OOP

C++ OOP

1. Fundamentals of OOP
2. Constructors and Destructors
3. Encapsulation Overview
4. Inheritance Overview
5. Polymorphism Overview

bookVirtual 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

copy
1234567
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.

cpp

main

copy
123456789101112131415161718192021222324252627
#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.

cpp

main

copy
12345678910111213141516171819202122232425262728293031
#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.

cpp

main

copy
1234567891011121314
#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.

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 2
We're sorry to hear that something went wrong. What happened?
some-alt