FreeBasic Virtual Keyword
last modified June 16, 2025
The FreeBasic Virtual keyword enables polymorphism by allowing
methods to be overridden in derived classes. It's fundamental for object-oriented
programming in FreeBasic. Virtual methods provide runtime method resolution.
Basic Definition
In FreeBasic, Virtual marks a method as overridable in derived
classes. When a virtual method is called, the actual implementation is
determined at runtime based on the object's type.
Virtual methods are declared in base classes and can be redefined in derived
classes using the Override keyword. This enables polymorphic
behavior where the same method call can produce different results.
Simple Virtual Method
This example demonstrates a basic virtual method declaration and override.
Type Animal
Declare Virtual Sub MakeSound()
End Type
Type Dog Extends Animal
Declare Sub MakeSound() Override
End Type
Sub Animal.MakeSound()
Print "Animal sound"
End Sub
Sub Dog.MakeSound()
Print "Woof!"
End Sub
Dim As Animal Ptr a = New Dog
a->MakeSound()
Delete a
Here we define an Animal base type with a virtual MakeSound
method. The Dog type overrides this method. When called through
a base class pointer, the derived class's implementation is executed.
Virtual Method with Parameters
Virtual methods can accept parameters just like regular methods.
Type Shape
Declare Virtual Function Area(w As Integer, h As Integer) As Integer
End Type
Type Rectangle Extends Shape
Declare Function Area(w As Integer, h As Integer) As Integer Override
End Type
Function Shape.Area(w As Integer, h As Integer) As Integer
Return 0
End Function
Function Rectangle.Area(w As Integer, h As Integer) As Integer
Return w * h
End Function
Dim As Shape Ptr s = New Rectangle
Print "Area: "; s->Area(10, 20)
Delete s
This example shows a virtual method with parameters. The base Shape
class provides a default implementation, while Rectangle overrides
it with a specific calculation. The correct version is called at runtime.
Virtual Destructor
Virtual destructors ensure proper cleanup of derived class resources.
Type Base
Declare Virtual Destructor()
Declare Virtual Sub DoSomething()
End Type
Type Derived Extends Base
Declare Destructor() Override
Declare Sub DoSomething() Override
End Type
Destructor Base()
Print "Base destructor"
End Destructor
Destructor Derived()
Print "Derived destructor"
End Destructor
Sub Base.DoSomething()
Print "Base doing something"
End Sub
Sub Derived.DoSomething()
Print "Derived doing something"
End Sub
Dim As Base Ptr b = New Derived
b->DoSomething()
Delete b
This code demonstrates the importance of virtual destructors. When deleting a derived object through a base pointer, the derived destructor is called first, followed by the base destructor. Without the virtual keyword, only the base destructor would be called.
Abstract Virtual Method
Virtual methods can be made abstract by not providing an implementation.
Type Animal
Declare Abstract Virtual Sub Speak()
End Type
Type Cat Extends Animal
Declare Sub Speak() Override
End Type
Sub Cat.Speak()
Print "Meow"
End Sub
Dim As Animal Ptr a = New Cat
a->Speak()
Delete a
Here, Animal declares an abstract virtual method Speak
that must be implemented by derived classes. Cat provides the
implementation. Abstract methods create interfaces that derived classes must follow.
Multiple Levels of Inheritance
Virtual methods work through multiple levels of class inheritance.
Type Grandparent
Declare Virtual Sub Greet()
End Type
Type Parent Extends Grandparent
Declare Sub Greet() Override
End Type
Type Child Extends Parent
Declare Sub Greet() Override
End Type
Sub Grandparent.Greet()
Print "Hello from Grandparent"
End Sub
Sub Parent.Greet()
Print "Hello from Parent"
End Sub
Sub Child.Greet()
Print "Hello from Child"
End Sub
Dim As Grandparent Ptr g = New Child
g->Greet()
Delete g
This example shows virtual method overriding through three levels of inheritance.
When called through a Grandparent pointer, the most derived
version (Child's implementation) is executed. This demonstrates
polymorphism in deep inheritance hierarchies.
Virtual Methods with Return Values
Virtual methods can return values of any type, including user-defined types.
Type Result
value As Integer
End Type
Type Calculator
Declare Virtual Function Compute(a As Integer, b As Integer) As Result
End Type
Type Adder Extends Calculator
Declare Function Compute(a As Integer, b As Integer) As Result Override
End Type
Function Calculator.Compute(a As Integer, b As Integer) As Result
Dim r As Result
r.value = 0
Return r
End Function
Function Adder.Compute(a As Integer, b As Integer) As Result
Dim r As Result
r.value = a + b
Return r
End Function
Dim As Calculator Ptr c = New Adder
Dim res As Result = c->Compute(5, 7)
Print "Result: "; res.value
Delete c
This code demonstrates a virtual method returning a user-defined type.
The base Calculator class provides a default implementation,
while Adder overrides it with specific functionality. The
return type remains consistent across all implementations.
Virtual Methods in Interfaces
Virtual methods are essential when working with interface-like patterns.
Type IDrawable
Declare Abstract Virtual Sub Draw()
End Type
Type Circle Extends IDrawable
Declare Sub Draw() Override
End Type
Sub Circle.Draw()
Print "Drawing a circle"
End Sub
Dim As IDrawable Ptr d = New Circle
d->Draw()
Delete d
This example shows how virtual methods can be used to create interface-like
behavior in FreeBasic. IDrawable defines a contract that
Circle implements. This pattern is useful for creating
pluggable components with well-defined behaviors.
Best Practices
- Destructors: Always make destructors virtual in base classes.
- Documentation: Clearly document virtual method contracts.
- Override: Use the Override keyword for clarity and safety.
- Abstract: Mark methods as abstract when they must be implemented.
- Performance: Be aware of the small overhead of virtual calls.
This tutorial covered the FreeBasic Virtual keyword with practical
examples showing its usage in different scenarios. Virtual methods enable
polymorphism, a cornerstone of object-oriented programming.
Author
List all FreeBasic Tutorials.