ZetCode

FreeBasic Class Keyword

last modified June 16, 2025

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.

simple_class.bas
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.

constructor_destructor.bas
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.

inheritance.bas
Type Animal
    Public:
        Dim dname As String
        Declare Sub speak()
End Type

Sub Animal.speak()
    Print "Animal sound"
End Sub

Type Dog Extends Animal
    Public:
        Declare Sub speak()
End Type

Sub Dog.speak()
    Print This.dname; " says: Woof!"
End Sub

Dim d As Dog
d.dname = "Rex"
d.speak()

Here, Dog inherits from Animal and overrides the speak method. The Extends keyword establishes the inheritance relationship. This demonstrates polymorphism in FreeBasic.

Class Properties

Properties provide controlled access to class fields with getters and setters.

properties.bas
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 can't be negative. Properties help maintain data integrity.

Static Class Members

Static members belong to the class itself rather than individual instances.

static_members.bas
Type Counter
    Public:
        Declare Static Sub increment()
        Declare Static Function getCount() As Integer
    Private:
        Static As Integer count
End Type

Static 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. Note the special syntax for static definitions.

Operator Overloading

Classes can define custom behavior for operators like +, -, *, etc.

operator_overloading.bas
Type Vector
    Public:
        Dim x As Single
        Dim y As Single
        
        Declare Operator Cast() As String
        Declare Operator + (ByRef v As Vector) As Vector
End Type

Operator Vector.Cast() As String
    Return "(" & This.x & ", " & This.y & ")"
End Operator

Operator Vector.+ (ByRef v As Vector) As Vector
    Dim result As Vector
    result.x = This.x + v.x
    result.y = This.y + v.y
    Return result
End Operator

Dim v1 As Vector = (1, 2)
Dim v2 As Vector = (3, 4)
Dim v3 As Vector = v1 + v2

Print "v1: "; v1
Print "v2: "; v2
Print "v1 + v2: "; v3

This Vector class overloads the + operator and provides a string conversion operator. Operator overloading allows natural syntax for custom types. The cast operator enables automatic conversion to strings for printing.

Abstract Classes

Abstract classes define interfaces that derived classes must implement.

abstract_class.bas
Type Shape Abstract
    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. Circle inherits from Shape and implements the required method. Abstract classes can't be instantiated directly.

Best Practices

This tutorial covered the FreeBasic Class keyword with practical examples showing object-oriented programming techniques in FreeBasic.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.