ZetCode

FreeBasic Constructor Keyword

Last modified April 20, 2026

The FreeBasic Constructor keyword defines special methods that initialize objects when they are created. Constructors are called automatically when an instance of a user-defined type is declared.

Basic Definition

In FreeBasic, a Constructor is a special member function of a Type (UDT) that is declared within the type definition and implemented outside of it. It is executed when an object of that type is created.

Constructors can take parameters to initialize object members. They ensure objects start in a valid state. FreeBasic supports both parameterless and parameterized constructors. The constructor is declared using Declare Constructor inside the Type, and defined using Constructor TypeName outside the Type.

Simple Constructor

This example shows a basic parameterless constructor.

simple_constructor.bas
Type Person
 fname As String
 age As Integer
 
 Declare Constructor()
End Type
Constructor Person()
 fname = "Unknown"
 age = 0
 Print "Person object created"
End Constructor
Dim p As Person
Print "Name: "; p.fname
Print "Age: "; p.age

Here we define a Person type with a constructor declaration inside the type and implementation outside. The Declare Constructor() goes inside the Type, while Constructor Person() contains the actual code. When we create a Person object, the constructor runs automatically, setting default values and printing a message.

Parameterized Constructor

Constructors can take parameters to initialize objects with specific values.

parameterized_constructor.bas
Type Rectangle
 width As Double
 height As Double
 
 Declare Constructor()
 Declare Constructor(w As Double, h As Double)
End Type
Constructor Rectangle()
 width = 0.0
 height = 0.0
End Constructor
Constructor Rectangle(w As Double, h As Double)
 width = w
 height = h
 Print "Rectangle created: "; width; "x"; height
End Constructor
Dim rect As Rectangle Ptr = New Rectangle(5.5, 3.2)
Print "Area: "; rect->width * rect->height
Delete rect

This Rectangle type demonstrates constructor overloading with both a default (parameterless) constructor and a parameterized constructor. To use the parameterized constructor with specific values, we create the object dynamically using New Rectangle(parameters). The parameterized constructor is called during allocation, initializing the object with the provided dimensions. We access members through the pointer and clean up with Delete.

Multiple Constructors

FreeBasic allows defining multiple constructors with different parameters.

multiple_constructors.bas
Type Book
 title As String
 author As String
 pages As Integer
 
 Declare Constructor()
 Declare Constructor(t As String, a As String, p As Integer)
End Type
Constructor Book()
 title = "Untitled"
 author = "Unknown"
 pages = 0
End Constructor
Constructor Book(t As String, a As String, p As Integer)
 title = t
 author = a
 pages = p
End Constructor
Dim book1 As Book
Dim book2 As Book Ptr = New Book("The Hobbit", "J.R.R. Tolkien", 310)
Print "Book1: "; book1.title; " by "; book1.author
Print "Book2: "; book2->title; " by "; book2->author
Delete book2

The Book type demonstrates constructor overloading by declaring two constructors with Declare Constructor. Each constructor is implemented separately with Constructor Book. The first book uses the default constructor automatically, while the second book uses dynamic allocation with New Book(parameters) to call the parameterized constructor. Note the different access patterns: direct member access for stack objects and pointer access (->) for heap objects.

Constructor with Member Initialization

Constructors can initialize members using more explicit syntax when needed.

member_init_constructor.bas
Type Point
 x As Integer
 y As Integer
 
 Declare Constructor(x As Integer, y As Integer)
End Type
Constructor Point(x As Integer, y As Integer)
 This.x = x
 This.y = y
End Constructor
Dim pt As Point Ptr = New Point(10, 20)
Print "Point coordinates: ("; pt->x; ", "; pt->y; ")"
Delete pt

This Point constructor demonstrates parameter name shadowing, where the constructor parameters have the same names as the type members. The This keyword is used to distinguish between the member variables (This.x, This.y) and the constructor parameters (x, y). The object is created dynamically using New Point(parameters), and we access members through the pointer with the arrow operator.

Constructor with Dynamic Allocation

Constructors work with dynamically allocated objects.

dynamic_constructor.bas
Type Student
 fname As String
 grade As Integer
 
 Declare Constructor(n As String, g As Integer)
End Type
Constructor Student(n As String, g As Integer)
 fname = n
 grade = g
 Print "Student "; fname; " created"
End Constructor
Dim s As Student Ptr = New Student("Alice", 11)
Print s->fname; " is in grade "; s->grade
Delete s

Here we create a Student object dynamically using New. The constructor is called automatically during allocation, initializing the object with the provided parameters. We access members through the pointer using the arrow operator (->) and must remember to Delete the object when done to free memory.

Constructor with Array Members

Constructors can initialize array members.

array_constructor.bas
Type Inventory
 items(4) As String
 count As Integer
 
 Declare Constructor()
End Type
Constructor Inventory()
 For i As Integer = 0 To 4
 items(i) = "Empty slot"
 Next
 count = 0
End Constructor
Dim inv As Inventory
For i As Integer = 0 To 4
 Print i; ": "; inv.items(i)
Next

The Inventory constructor shows how to initialize array members within a constructor. The parameterless constructor loops through all array elements, setting each to "Empty slot", and initializes the count to zero. This ensures that when an inventory object is created, all slots start in a known, initialized state.

Constructor with Object Composition

Constructors can initialize objects that contain other objects.

composition_constructor.bas
Type Engine
 horsepower As Integer
 
 Declare Constructor()
 Declare Sub SetHorsepower(hp As Integer)
End Type
Constructor Engine()
 horsepower = 0
End Constructor
Sub Engine.SetHorsepower(hp As Integer)
 horsepower = hp
End Sub
Type Car
 model As String
 engine As Engine
 
 Declare Constructor(m As String, hp As Integer)
End Type
Constructor Car(m As String, hp As Integer)
 model = m
 engine.SetHorsepower(hp)
End Constructor
Dim myCar As Car Ptr = New Car("Sedan", 150)
Print myCar->model; " has "; myCar->engine.horsepower; " HP"
Delete myCar

This example demonstrates object composition where the Car type contains an embedded Engine object. The Engine type has a default constructor and a SetHorsepower method to configure its properties. The car is created dynamically using New Car(parameters), which calls the car's constructor. The constructor initializes the model name and uses the engine's SetHorsepower method to configure the embedded engine object. This approach shows how to work with composition when direct parameterized constructor calls are complex.

Best Practices

  • Initialization: Always initialize all member variables in constructors.
  • Overloading: Use multiple constructors for different initialization scenarios.
  • Validation: Validate constructor parameters when appropriate.
  • Simplicity: Keep constructors simple; avoid complex logic.
  • Documentation: Document constructor behavior and parameters.

This tutorial covered the FreeBasic Constructor keyword with practical examples showing its usage in different scenarios.

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.

AltStyle によって変換されたページ (->オリジナル) /