Simple class example

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 74
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Simple class example

Post by Löwenherz »

Hello all have Made today this little class example If you have another approach its OK its my First time to build such an oop example
Critics are Welcome
regards Frank

Code: Select all

' class example by frank bruebach, freebasic, 16-04-2024
'
' Define the Employee class
Type Employee
    names As String
    dept As String
    salary As Integer
End Type

' Define the constructor for the Employee class
Sub Employee_Constructor(ByRef emp As Employee, names As String, dept As String, salary As Integer)
    emp.names = names
    emp.dept = dept
    emp.salary = salary
End Sub

' Define the destructor for the Employee class
Sub Employee_Destructor(ByRef emp As Employee)
    ' Optionally 
End Sub

' Initialize an instance of the Employee class using the constructor
Dim john As Employee
Employee_Constructor(john, "John", "Computer Lab", 1000)

' Define methods to access and manipulate the attributes of the Employee class
Function getDepartment(emp As Employee) As String
    getDepartment = emp.dept
End Function

Function getSalary(emp As Employee) As Integer
    getSalary = emp.salary
End Function

Sub setSalary(ByRef emp As Employee, newSalary As Integer)
    emp.salary = newSalary
End Sub

' Print the attributes of the Employee instance
Print "Department: " & getDepartment(john)  ' Output: Computer Lab
Print "Salary: " & getSalary(john)          ' Output: 1000

' Update the salary of the Employee instance
setSalary(john, 1200)
Print "Updated Salary: " & getSalary(john)  ' Output: 1200

' Call the destructor for the Employee instance
Employee_Destructor(john)
Sleep 5000 
paul doe
Moderator
Posts: 1745
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: Simple class example

Post by paul doe »

FreeBasic supports many neat constructs when you deal with objects, such as constructors, destructors and also properties, and they can all be assigned access rights as well:

Code: Select all

type Employee
  public:
    declare constructor()
    declare constructor( as string, as string, as integer )
    declare destructor()
    
    '' Get
    declare property names() as string
    declare property dept() as string
    declare property salary() as integer
    
    '' Set
    declare property names( as string )
    declare property dept( as string )
    declare property salary( as integer )
  
  private:
    _names as string
    _dept as string
    _salary as integer
end type

'' Default constructor is useful for declaring arrays of simple types that
'' can have some default initializer
constructor Employee()
  constructor( "<UNKNOWN>", "<UNKNOWN>", 0 )
end constructor

constructor Employee( nnames as string, ndept as string, nsalary as integer )
  _names = nnames
  _dept = ndept
  _salary = nsalary
end constructor

destructor Employee()
  '' Optional, useful if the class allocated memory to free it automatically
end destructor

property Employee.names() as string
  return( _names )
end property

property Employee.dept() as string
  return( _dept )
end property

property Employee.salary() as integer
  return( _salary )
end property

property Employee.names( value as string )
  _names = value
end property

property Employee.dept( value as string )
  _dept = value
end property

property Employee.salary( value as integer )
  _salary = value
end property

'' Then you can simply do:
var emp = Employee( "John Doe", "Computer Lab", 1000 )

? emp.names, emp.dept, emp.salary
?

'' Or with arrays:
dim as Employee employees( ... ) = { _
  Employee( "John", "CS", 1000 ), _
  Employee( "Mary", "CS", 1200 ), _
  Employee( "Carl", "Security", 800 ) }

for i as integer = 0 to ubound( employees )
  with employees( i )
    ? .names, .dept, .salary
  end with
next

'' Let's give John a 20% raise in his salary, because why not
emp.salary = emp.salary * 1.20

?
? emp.names, emp.dept, emp.salary

sleep()
The constructs work transparenty with the language, as you can see.
Löwenherz
Posts: 74
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Simple class example

Post by Löwenherz »

Many thanks Paul Works Here fine :)
FreeBasic supports many neat constructs when you deal with objects, such as constructors, destructors and also properties, and they can all be assigned access rights as well:
In my eyes this Setup in freebasic its a little too complicated (my opiniion) but its running fine and good...Made a little enhancement with Kids

Code: Select all

type Employee
  public:
    declare constructor()
    declare constructor( as string, as string, As String, As integer )
    declare destructor()
    
    '' Get
    declare property names() as string
    declare property dept() as string
    declare property childs() as string
    declare property salary() as integer
    
    '' Set
    declare property names( as string )
    declare property dept( as string )
    declare property childs( as String )
    declare property salary( as integer )
  
  private:
    _names as string
    _dept as String
    _childs as String
    _salary as integer
end type

'' Default constructor is useful for declaring arrays of simple types that
'' can have some default initializer
constructor Employee()
  constructor( "<UNKNOWN>", "<UNKNOWN>","<UNKNOWN>", 0 )
end constructor

constructor Employee( nnames as string, ndept as string, nchilds As String, nsalary as integer )
  _names = nnames
  _dept = ndept
  _childs = nchilds
  _salary = nsalary
end constructor

destructor Employee()
  '' Optional, useful if the class allocated memory to free it automatically
end destructor

property Employee.names() as string
  return( _names )
end property

property Employee.dept() as string
  return( _dept )
end property

property Employee.childs() as string
  return( _childs )
end property

property Employee.salary() as integer
  return( _salary )
end property

property Employee.names( value as string )
  _names = value
end property

property Employee.dept( value as string )
  _dept = value
end property

property Employee.childs( value as string )
  _childs = value
end property

property Employee.salary( value as integer )
  _salary = value
end property

'' Then you can simply do:
var emp = Employee( "John Doe", "Computer Lab", "kids", 1000 )

? emp.names, emp.dept, emp.childs, emp.salary
?

'Print emp.names, emp.dept, emp.salary

'' Or with arrays:
dim as Employee employees( ... ) = { _
  Employee( "John", "CS","kids", 1000 ), _
  Employee( "Mary", "CS","kids", 1200 ), _
  Employee( "toby", "CS","kids", 50 ), _
  Employee( "Carl", "Security","kids", 800 ) }

for i as integer = 0 to ubound( employees )
  with employees( i )
    ? .names, .dept, .childs, .salary
  end with
next

'' Let's give John a 20% raise in his salary, because why not
emp.salary = emp.salary * 1.20

?
? emp.names, emp.dept, emp.childs, emp.salary

sleep()
Post Reply