Constructor Copy

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

Constructor Copy

Post by Löwenherz »

Hello all

Try to understand more about class constructor and have build this simple example and a Copy Feature but its Not using this and callocate Statements

Code: Select all

' -- class example 2 by frank bruebach, freebasic
' -- constructor copy
'
Type Superhero
  _name As String
  
  Declare Constructor ()
  Declare Constructor (names As String)
  Declare Constructor (other As Superhero)
  
  Declare Function Calculate(value1 As Integer, value2 As Integer) As Integer
  
  Declare Operator Cast () As String
  
  Declare Sub Cleanup()
End Type

Constructor Superhero ()
  _name = "Batman"
End Constructor

Constructor Superhero (names As String)
  _name = names
End Constructor

Constructor Superhero (other As Superhero)
  _name = other._name
End Constructor

Function Superhero.Calculate(value1 As Integer, value2 As Integer) As Integer
  Return value1 + value2
End Function

Operator Superhero.Cast () As String
  Return _name & " is my Hero"
End Operator

Sub Superhero.Cleanup()
  ' Add cleanup tasks here if needed
  ' This method will be called automatically 
  ' before the object is destroyed
End Sub

Print "Creating myHero"
Dim myHero As Superhero

Print "Creating copiedHero"
Dim copiedHero As Superhero = myHero

Print "Original Hero: "; myHero
Print "Copied Hero: "; copiedHero

Dim result As Integer = myHero.Calculate(5, 7)
Print "Calculation Result: "; result

' Call cleanup method explicitly before destroying the objects
myHero.Cleanup()
copiedHero.Cleanup()

Sleep()
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

- Instead of defining a 'cleanup()' method, you can define a 'destructor()' which will be automatically called before the object is destroyed.
- The 'calculate()' method does not use any member fields. So you can declare it as static and simply call it on the type name and not necessarily on the name of an instance.

Code: Select all

Type Superhero
  _name As String
  
  Declare Constructor ()
  Declare Constructor (names As String)
  Declare Constructor (other As Superhero)
  
  Declare Static Function Calculate(value1 As Integer, value2 As Integer) As Integer
  
  Declare Operator Cast () As String
  
  Declare Destructor()
End Type

Constructor Superhero ()
  _name = "Batman"
End Constructor

Constructor Superhero (names As String)
  _name = names
End Constructor

Constructor Superhero (other As Superhero)
  _name = other._name
End Constructor

Function Superhero.Calculate(value1 As Integer, value2 As Integer) As Integer
  Return value1 + value2
End Function

Operator Superhero.Cast () As String
  Return _name & " is my Hero"
End Operator

Destructor Superhero()
  ' Add cleanup tasks here if needed
  ' This body will be called automatically 
  ' before the object is destroyed
End Destructor

Print "Creating myHero"
Dim myHero As Superhero

Print "Creating copiedHero"
Dim copiedHero As Superhero = myHero

Print "Original Hero: "; myHero
Print "Copied Hero: "; copiedHero

Dim result As Integer = Superhero.Calculate(5, 7)
Print "Calculation Result: "; result

Sleep()
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Constructor Copy

Post by Löwenherz »

Thank you fmx :)

Perhaps you can correct my next enhanced example I have built Problem zones I have marked , thanks

Code: Select all

' -- little enhencement but with errors
' -- I have marked problem zones :-)
'
Type Friend
  _name As String
End Type

Type Superhero
  _name As String
  _powerLevel As Integer
  
  Declare Constructor ()
  Declare Constructor (names As String, powerLevel As Integer)
  Declare Constructor (other As Superhero)
  Declare Function Calculate(value1 As Integer, value2 As Integer) As Integer
  Declare Function Calculate(value2 As Integer) As Integer
  
  Declare Destructor ()
  
  'Function Calculate(value2 As Integer) As Integer
  '  Return _powerLevel * value2
  'End Function
  
  Declare Operator Cast () As String
End Type
  
Constructor Superhero ()
  _name = "Batman"
  _powerLevel = 10
End Constructor

Constructor Superhero (names As String, powerLevel As Integer)
  _name = names
  _powerLevel = powerLevel
End Constructor

Constructor Superhero (other As Superhero)
  _name = other._name
  _powerLevel = other._powerLevel
End Constructor

Destructor Superhero ()
  ' Add cleanup tasks here if needed
  ' This destructor will be called automatically before the object is destroyed
End Destructor

Operator Superhero.Cast () As String
  Return _name & " is my Hero"
End Operator

Type Superman
  As Superhero ' problem zone 1
  
  Declare Constructor ()
  Declare Constructor (names As String, powerLevel As Integer)
  
  ' problem zone 2 ------------------------------- //
  Function Calculate(value2 As Integer) As Integer
    Return _powerLevel * value2
  End Function
  ' problem zone 2 ------------------------------- //
  
End Type

Constructor Superman ()
  Superhero.Constructor("Superman", 20) 'problem 3
End Constructor

Constructor Superman (names As String, powerLevel As Integer)
  Superhero.Constructor(names, powerLevel) ''problem 4
End Constructor

Print "Creating Batman"
Dim Batman As Superhero

Print "Creating Superman"
Dim Superman As Superhero ''problem 5

Print "Batman's Power Level: "; Batman._powerLevel
Print "Superman's Power Level: "; Superman._powerLevel ''problem 6

Dim result1 As Integer = Batman.Calculate(5)
Print "Batman's Calculation Result: "; result1

Dim result2 As Integer = Superman.Calculate(5) ''problem 7
Print "Superman's Calculation Result: "; result2
Sleep()
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

If I knew what you wanted to do with the second 'Superman' type in addition to the first 'SuperHero' type, maybe I could help you?
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Constructor Copy

Post by Löwenherz »

If I knew what you wanted to do with the second 'Superman' type in addition to the first 'SuperHero' type, maybe I could help you

Yesss feel free to Help :-D
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

Löwenherz wrote: Apr 20, 2024 15:40
If I knew what you wanted to do with the second 'Superman' type in addition to the first 'SuperHero' type, maybe I could help you

Yesss feel free to Help :-D

I wrote that I could help you only if I understood what you want to do with the second 'Superman' type compared to the first 'Superhero' type.
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Constructor Copy

Post by Löwenherz »

I wanted only that the act both as class friend together and do some different calculations at the end nothung More was Just an Idea
I am Reading an old comic book with Batman :)
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

For example:

Code: Select all

Type Superhero
  _name As String
  _powerLevel As Integer
  
  Declare Constructor ()
  Declare Constructor (names As String, powerLevel As Integer)
  Declare Constructor (other As Superhero)
  Declare Function Calculate(value2 As Integer) As Integer
  
  Declare Destructor ()
  
  Declare Operator Cast () As String
End Type
  
Constructor Superhero ()
End Constructor

Constructor Superhero (names As String, powerLevel As Integer)
  _name = names
  _powerLevel = powerLevel
End Constructor

Constructor Superhero (other As Superhero)
  _name = other._name
  _powerLevel = other._powerLevel
End Constructor

Function Superhero.Calculate(value2 As Integer) As Integer
  Return _powerLevel * value2
End Function

Destructor Superhero ()
  ' Add cleanup tasks here if needed
  ' This destructor will be called automatically before the object is destroyed
End Destructor

Operator Superhero.Cast () As String
  Return _name
End Operator

Print "Creating Batman"
Dim Batman As Superhero = Superhero("Batman", 10)

Print "Creating Superman"
Dim Superman As Superhero = Superhero("Superman", 20)

Print Batman & "'s Power Level: "; Batman._powerLevel
Print Superman & "'s Power Level: "; Superman._powerLevel

Print Batman & "'s Calculation Result: "; Batman.Calculate(5)
Print Superman & "'s Calculation Result: "; Superman.Calculate(5)

Sleep()
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Constructor Copy

Post by Löwenherz »

Yes your example runs Well now I know thats a function must be outside of a Type Statement

I have combined my example and your correction for a new example thanks fxm for help

Code: Select all

' final class example combined both together 
' calculation and addition, batman, superman
'
Type Friend
  _name As String
End Type

Type Superhero
  _name As String
  _powerLevel As Integer
  
  Declare Constructor ()
  Declare Constructor (names As String, powerLevel As Integer)
  Declare Constructor (other As Superhero)
  Declare Function Calculate(value1 As Integer, value2 As Integer) As Integer
  Declare Function Calculate(value3 As Integer) As Integer
  
  Declare Destructor ()
  
  Declare Operator Cast () As String
End Type
  
Constructor Superhero ()
   _name = "Batman"
   _powerLevel = 10
End Constructor

Constructor Superhero (names As String, powerLevel As Integer)
  _name = names
  _powerLevel = powerLevel
End Constructor

Constructor Superhero (other As Superhero)
  _name = other._name
  _powerLevel = other._powerLevel
End Constructor

Function Superhero.Calculate(value1 As Integer, value2 As integer) As Integer
  Return value1+value2
End Function

Function Superhero.Calculate(value3 As Integer) As Integer
  Return _powerLevel * value3
End Function

Destructor Superhero ()
  ' Add cleanup tasks here if needed
  ' This destructor will be called automatically before the object is destroyed
End Destructor

Operator Superhero.Cast () As String
  Return _name & " has it's own real Hero"
End Operator

Print "Creating myRealHero"
Dim myHero As Superhero

Print "Creating copiedHero"
Dim copiedHero As Superhero = myHero

Print "Original Hero: "; myHero
Print "Copied Hero: "; copiedHero

Print "Creating Batman"
Dim Batman As Superhero = Superhero("Batman", 10)

Print "Creating Superman"
Dim Superman As Superhero = Superhero("Superman", 20)

Print Batman & "'s Power Level: "; Batman._powerLevel
Print Superman & "'s Power Level: "; Superman._powerLevel

Print Batman & "'s Calculation Result: "; Batman.Calculate(5)
Print Superman & "'s Calculation Result: "; Superman.Calculate(5)

Dim result As Integer = myHero.Calculate(5, 7)
Print "Calculation Result: "; result

Sleep()
PS I've learned something again and every basic language works differently, it seems
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

Note:
You can add the code tags by pressing the "</>" button on the selected code body.
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

In FreeBASIC, constructors, destructor, properties, member operators, member procedures, must all be declared inside the 'Type...End Type' structure, but must all be defined with their code body in outside this structure (this may perhaps change in the future).
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Constructor Copy

Post by Löwenherz »

Thanks fxm for explanations
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

If the topic "constructors, copy constructor, copy assignment, destructor" interests you especially, you can start by reading in the Programmer's Guide:
- first the User Defined Types / Constructors and Destructors (basics) page,
- then the User Defined Types / Constructors, '=' Assignment-Operators, and Destructors (advanced, part #1) page,
- and finally if you still have the courage the User Defined Types / Constructors, '=' Assignment-Operators, and Destructors (advanced, part #2) page.
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Constructor Copy

Post by Löwenherz »

Hello yes thx again for Infos and links fxm will Check more later but its Always Welcome Here :)

Have made this new little example with constructor and Copy constructor I have Got this now

Code: Select all


' - freebasic, constructor copy example
' - by frank bruebach, 21-04-2024
'
Type Persona
    _firstName As String
    _age As Integer
    
    Declare Constructor ()
    Declare Constructor (fName As String, abc As Integer)
    Declare Constructor (other As persona)
    
    Declare Destructor ()
End Type

    Constructor persona()
        _firstName = ""
        _age = 0
    End Constructor
    
    Constructor persona (fName As String, a As Integer)
        _firstName = fName
        _age = a
    End Constructor
    
    Constructor persona (other As Persona)
        _firstName = other._firstName
        _age = other._age
    End Constructor

Destructor persona ()
  ' Add cleanup tasks here if needed
  ' This destructor will be called automatically before the object is destroyed
End Destructor

Print "Creating myPersona"
Dim myPer As Persona
Dim myPer2 As Persona

myPer = Persona("Alice", 30)
myPer2 = Persona(myPer) ' Copy constructor used to initialize p2 with the values of myPer

Print "Persona 1: " & myPer._firstName & " - Age: " & myPer._age
Print "Persona 2: " & myPer2._firstName & " - Age: " & myPer._age
    
Sleep()
fxm
Moderator
Posts: 12145
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Constructor Copy

Post by fxm »

In your case of member data fields ('Integer' and 'String'), defining a copy-constructor by the user is useless because the compiler can itself manage the copy of these member data fields between 2 instances. In the case of member var-len string or member var-len array for example, it itself generates an implicit copy-constructor because a shallow copy is not enough (because only the descriptor is inside the Type, not the data).

Defining a copy-constructor by the user is particularly the case when a data object is allocated dynamically by the user (only a member pointer is in the Type for object aggregation). A shallow copy of the field of one object in another would only copy the pointer value, not the data pointed. Thus, changing this data for one object would result in the modification of the other object's data, which would probably not be the desired effect.

Similar remark for the copy-assignment operator ('Declare Operator Let (other As persona)' in your case).

Purely academic example where the user absolutely must define a copy-constructor and a copy-assignment operator
(you can remove the copy-constructor and/or copy-assignment operator by putting in comment the line 1 and/or line 2)

Code: Select all

#define copyConstruction
#define copyAssignment

Type DynamicString
    Public:
        Declare Operator Cast() As String
        Declare Constructor()
        Declare Constructor(Byref s0 As String)
        Declare Operator Let(Byref s0 As String)
        #ifdef copyConstruction
        Declare Constructor(Byref ds0 As DynamicString)
        #endif
        #ifdef copyAssignment
        Declare Operator Let(Byref ds0 As DynamicString)
        #endif
    Private:
        Dim As String s
        Dim As String Ptr ps
End Type

Operator DynamicString.Cast() As String
    Return *This.ps
End Operator

Constructor DynamicString()
    This.ps = @This.s
End Constructor

Constructor DynamicString(Byref s0 As String)
    Constructor()
    *This.ps = s0
End Constructor

Operator DynamicString.Let(Byref s0 As String)
    *This.ps = s0
End Operator

#ifdef copyConstruction
Constructor DynamicString(Byref ds0 As DynamicString)
    Constructor()
    *This.ps = *ds0.ps
End Constructor
#endif

#ifdef copyAssignment
Operator DynamicString.Let(Byref ds0 As DynamicString)
    *This.ps = *ds0.ps
End Operator
#endif

Dim As DynamicString ds1 = "Free"
Dim As DynamicString ds2 = ds1
Print ds2
ds2 = "BASIC"
Dim As DynamicString ds3
ds3 = ds2
Print ds3
ds3 = "1.20.0"
Print ds3
Print ds1 & ds2 & " " & ds3
Print

Sleep
Last edited by fxm on Apr 25, 2024 9:01, edited 3 times in total.
Reason: Added example.
Post Reply