C++ Virtual Functions šŸŽÆ

beginner
9 min

C++ Virtual Functions šŸŽÆ

Welcome to our deep dive into C++ Virtual Functions! In this lesson, we'll explore how virtual functions work, why they're crucial, and how to use them effectively. Let's get started!

What are Virtual Functions? šŸ“

Virtual functions, also known as virtual methods or polymorphic functions, are functions in C++ that can be overridden by derived classes. They allow objects of a derived class to be treated as if they were objects of the base class.

Why are Virtual Functions important? šŸ’”

Virtual functions enable us to write more flexible and reusable code by allowing different derived classes to implement their own versions of a base class function. This makes it possible to use a base class pointer to call a method that may behave differently depending on the actual object being pointed to.

How do Virtual Functions work? šŸ“

A virtual function is declared in the base class with the virtual keyword. When a derived class overrides the function, it's said to be overriding the base class version.

Here's a simple example:

cpp
// Base class class Shape { public: virtual void draw() { std::cout << "Drawing a generic shape\n"; } }; // Derived class class Square : public Shape { public: void draw() { std::cout << "Drawing a square\n"; } };

In this example, we have a Shape base class with a draw() virtual function. The Square class inherits from Shape and overrides the draw() function.

Using Virtual Functions šŸ“

To use virtual functions, you can create a pointer or a reference to the base class and assign it an object of a derived class. When you call a virtual function on the base class pointer or reference, the appropriate derived class version of the function will be executed.

cpp
int main() { Square square; Shape* shape = &square; // Create a pointer to the Shape class and assign it the square object shape->draw(); // Call the draw() function through the Shape pointer, but it will execute the Square version return 0; }

Output:

Drawing a square

Polymorphism šŸ’”

Polymorphism, the ability of objects to take multiple forms, is a fundamental concept in object-oriented programming. Virtual functions enable polymorphism by allowing derived classes to provide their own implementation of base class functions.

Virtual Function Types šŸ“

C++ has two types of virtual functions:

  1. Pure Virtual Functions (= 0) – These functions don't have an implementation in the base class and must be overridden by derived classes. They're denoted using the = 0 syntax.
cpp
class AbstractShape { public: virtual void draw() = 0; // Pure virtual function };
  1. Static Virtual Functions (static) – These functions are not virtual by default and must be explicitly declared as virtual if they need to be overridden in derived classes. Static virtual functions are bound at compile-time, not at runtime.
cpp
class Shape { public: static void printCount() { std::cout << "Number of shapes: " << count << "\n"; } static int count; // Static variable for the total number of shapes }; int Shape::count = 0; // Initializing the static variable

Virtual Function Destruction šŸ’”

When a derived class object is destroyed, the destructor of the base class and then the destructor of the derived class are called. If the derived class overrides the destructor, it's executed before the base class destructor.

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

Which of the following statements is true about virtual functions in C++?


That's all for now! In the next lesson, we'll delve deeper into virtual functions, covering topics like function overriding, function overloading, and runtime polymorphism. Stay tuned! šŸš€