FreeBasic Class Keyword
Last modified October 18, 2026
The FreeBasic Class keyword enables object-oriented programming by
defining custom types that encapsulate data and behavior. Classes are blueprints
for creating objects with properties and methods.
Basic Definition
In FreeBasic, Class declares a user-defined type that can contain
member variables (fields) and member functions (methods). Classes support
encapsulation, inheritance, and polymorphism.
Class members can have different access levels: Public, Private, or Protected. Public members are accessible everywhere, while Private members are only accessible within the class.
Simple Class Definition
This example demonstrates a basic class with properties and methods.
Type Person
Public:
Dim fname As String
Dim age As Integer
Declare Sub greet()
Private:
Dim secret As String = "My secret"
End Type
Sub Person.greet()
Print "Hello, my name is "; This.fname
Print "I am "; This.age; " years old"
End Sub
Dim p As Person
p.fname = "John"
p.age = 30
p.greet()
Here we define a Person class with public fields name
and age, a private field secret, and a public method
greet. The This keyword refers to the current object
instance.
Class Constructors and Destructors
Classes can have special methods called constructors and destructors.
Type Book
Public:
Dim title As String
Dim author As String
Declare Constructor()
Declare Constructor(t As String, a As String)
Declare Destructor()
End Type
Constructor Book()
This.title = "Untitled"
This.author = "Unknown"
End Constructor
Constructor Book(t As String, a As String)
This.title = t
This.author = a
End Constructor
Destructor Book()
Print "Book '"; This.title; "' is being destroyed"
End Destructor
Dim b1 As Book
Dim b2 As Book = Book("The Hobbit", "J.R.R. Tolkien")
Print b1.title; " by "; b1.author
Print b2.title; " by "; b2.author
This example shows two constructors (one default and one parameterized) and a destructor. Constructors initialize objects, while the destructor is called when objects are destroyed. FreeBasic automatically calls the destructor.
Class Inheritance
Classes can inherit from other classes to create hierarchical relationships.
Type Animal Extends Object
Public:
Dim dname As String
Declare Virtual Sub speak()
End Type
Sub Animal.speak()
Print "Animal sound"
End Sub
Type Dog Extends Animal
Public:
Declare Virtual Sub speak()
End Type
Sub Dog.speak()
Print This.dname; " says: Woof!"
End Sub
Dim d As Dog
d.dname = "Rex"
d.speak()
Dim a As Animal Ptr = @d
a->speak()
Here, Dog inherits from Animal and overrides the
speak method. The Extends keyword establishes the
inheritance relationship. The methods are declared Virtual, so a
call through an Animal pointer dispatches to Dog.speak
at runtime; this demonstrates polymorphism. Without Virtual, the
base method would be called instead. The program prints
Rex says: Woof! twice.
Class Properties
Properties provide controlled access to class fields with getters and setters.
Type Circle
Private:
Dim radius As Single
Public:
Declare Property r() As Single
Declare Property r(ByVal value As Single)
End Type
Property Circle.r() As Single
Return This.radius
End Property
Property Circle.r(ByVal value As Single)
If value >= 0 Then
This.radius = value
Else
This.radius = 0
End If
End Property
Dim c As Circle
c.r = 5.5
Print "Radius: "; c.r
c.r = -2
Print "Radius: "; c.r
This Circle class uses properties to control access to its
radius field. The setter property includes validation to ensure
the radius cannot be negative. Properties help maintain data integrity.
Static Class Members
Static members belong to the class itself rather than individual instances.
Type Counter
Public:
Dim As Integer id
Declare Static Sub increment()
Declare Static Function getCount() As Integer
Private:
Static As Integer count
End Type
Dim As Integer Counter.count = 0
Static Sub Counter.increment()
Counter.count += 1
End Sub
Static Function Counter.getCount() As Integer
Return Counter.count
End Function
Counter.increment()
Counter.increment()
Counter.increment()
Print "Count: "; Counter.getCount()
The Counter class demonstrates static members. The count
variable is shared among all instances. Static methods are called on the class
itself rather than on objects. The static variable is declared inside the type
and must be defined outside it with a Dim statement. The
type also needs at least one non-static member; a type containing only static
members fails to compile. Running the program prints Count: 3.
Operator Overloading
Classes can define custom behavior for operators like +, -, *, etc.
Type Vector
Public:
Dim x As Single
Dim y As Single
Declare Constructor (ByVal nx As Single = 0, ByVal ny As Single = 0)
Declare Operator Cast() As String
End Type
Constructor Vector (ByVal nx As Single, ByVal ny As Single)
This.x = nx
This.y = ny
End Constructor
Operator Vector.Cast() As String
Return "(" & Str(This.x) & ", " & Str(This.y) & ")"
End Operator
Operator + (ByRef a As Vector, ByRef b As Vector) As Vector
Return Vector(a.x + b.x, a.y + b.y)
End Operator
Dim v1 As Vector = Vector(1, 2)
Dim v2 As Vector = Vector(3, 4)
Dim v3 As Vector = v1 + v2
Print "v1: "; v1
Print "v2: "; v2
Print "v1 + v2: "; v3
This Vector class provides a constructor, a string conversion
operator, and a free-standing + operator. FreeBASIC does not allow
operators to be declared as member functions, so the + operator is
defined outside the type. The cast operator enables automatic conversion to
strings for printing. The program prints v1: (1, 2),
v2: (3, 4), and v1 + v2: (4, 6).
Abstract Classes
Abstract classes define interfaces that derived classes must implement.
Type Shape Extends Object
Public:
Declare Abstract Function area() As Single
End Type
Type Circle Extends Shape
Public:
Dim radius As Single
Declare Function area() As Single
End Type
Function Circle.area() As Single
Return 3.14159 * This.radius * This.radius
End Function
Dim c As Circle
c.radius = 5
Print "Circle area: "; c.area()
The Shape class is abstract with an abstract area
method. It must extend Object to declare abstract methods.
Circle inherits from Shape and implements
the required method. Declaring a variable of an abstract type, such as
Dim s As Shape, produces a compile error.
Best Practices
- Encapsulation: Keep fields private and expose via methods/properties.
- Single Responsibility: Each class should have one clear purpose.
- Naming: Use PascalCase for class names and camelCase for members.
- Composition: Prefer composition over inheritance when possible.
- Documentation: Document public interfaces clearly.
This tutorial covered the FreeBasic Class keyword with practical
examples showing object-oriented programming techniques in FreeBasic.
Author
List all FreeBasic Tutorials.