FB OOP Inheritence

General FreeBASIC programming questions.
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

FB OOP Inheritence

Postby MystikShadows » Dec 17, 2019 21:40

OK It's probably been asked before. But How do I inherit?

Scenario. BaseItem is a class in it's own module. contactItem is a baseItem I want to inherit from .. it has it's own module as well. how to I make that work? if i include once the baseitem I get messaged that multiple declarations exist. if I don't, it tells me Identifier expect found BaseItem.

So how am i gonna make this work? or is there a better way?

I want to create a contact object that is a BaseItem plus all the properties of a typical Contact object here. that's the goal. :)
Imortis
Moderator
Posts: 1725
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FB OOP Inheritence

Postby Imortis » Dec 17, 2019 22:00

Code: Select all

Type Base extends Object 'Needed to enable OOP for this UDT
     int1 as Integer
End Type

Type Extender Extends Base 'Now extend from previous UDT.
   int2  as Integer
End Type
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Re: FB OOP Inheritence

Postby MystikShadows » Dec 17, 2019 22:08

I'm trying that right now. and gives me the same warning whether I put it there or not. duplicate definitions of Base Item. or identifier expectedf BaseItem found. That's true whether BaseItem Extends Object of not.
Imortis
Moderator
Posts: 1725
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FB OOP Inheritence

Postby Imortis » Dec 17, 2019 22:10

Can you share some sample code? I can take a look and see what the issue it. Also, what version of FB are you using?
fxm
Posts: 9820
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FB OOP Inheritence

Postby fxm » Dec 17, 2019 22:12

I assume that your 2 modules are compiled as 2 separate files (and not one included in the other).
So, only the declarations of the base type (Type BaseItem ... End Type) must be duplicated in the two modules, not the definitions of the procedures.
Last edited by fxm on Dec 17, 2019 22:16, edited 1 time in total.
badidea
Posts: 2120
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: FB OOP Inheritence

Postby badidea » Dec 17, 2019 22:13

Try "Base1" (or some other name) instead "Base". Code of Imortis above is not 'real code'.
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Re: FB OOP Inheritence

Postby MystikShadows » Dec 17, 2019 22:17

BaseItemClass:

Code: Select all

TYPE BaseItemClass EXTENDS Object
Private:
     iUserID           AS INTEGER
     iItemID           as integer
     iParentID         AS INTEGER
     iItemTypeID       AS INTEGER
     sItemName         as sTRING * 60
     sDescription      AS STRING * 80
     iRelatedID        AS INTEGER
     sDateCreated      as STRING * 11
     sTimeCreated      AS STRING * 11
     iIsGrouping       AS INTEGER
     iIsActive         AS INTEGER
     
Public:
     DECLARE CONSTRUCTOR()
     DECLARE DESTRUCTOR()
     
     DECLARE PROPERTY SetUserID(Value AS INTEGER)
     DECLARE PROPERTY GetUserID() AS INTEGER
     DECLARE PROPERTY SetItemID(Value AS INTEGER)
     DECLARE PROPERTY GetItemID() AS INTEGER
     DECLARE PROPERTY SetParentID(Value AS INTEGER)
     DECLARE PROPERTY GetParentID() AS INTEGER
     DECLARE PROPERTY SetItemTypeID(Value AS INTEGER)
     DECLARE PROPERTY GetItemTypeID() AS INTEGER
     DECLARE PROPERTY SetItemName(VALUE AS STRING)
     DECLARE PROPERTY GetItemName() AS STRING
     DECLARE PROPERTY SetDescription(VALUE AS STRING)
     DECLARE PROPERTY GetDescription() AS STRING
     DECLARE PROPERTY SetRelatedID(Value AS INTEGER)
     DECLARE PROPERTY GetRelatedID() AS INTEGER
     DECLARE PROPERTY SetDateCreated(VALUE AS STRING)
     DECLARE PROPERTY GetDateCreated() AS STRING
     DECLARE PROPERTY SetTimeCreated(VALUE AS STRING)
     DECLARE PROPERTY GetTimeCreated() AS STRING
     DECLARE PROPERTY SetIsGrouping(Value AS INTEGER)
     DECLARE PROPERTY GetIsGrouping() AS INTEGER
     DECLARE PROPERTY SetIsActive(Value AS INTEGER)
     DECLARE PROPERTY GetIsActive() AS INTEGER
     
     declare function LoadItem(RecordNumber AS INTEGER) AS INTEGER
     DECLARE FUNCTION SaveItem() AS INTEGER
     
end type



ContactItemClass:

Code: Select all

#INCLUDE ONCE "BaseItemClass.bi"

TYPE ContactItemClass EXTENDS BaseItemClass
PRIVATE:
     ContactID        AS INTEGER

PUBLIC:
     DECLARE CONSTRUCTOR()
     DECLARE DESTRUCTOR()
     
end type


And just in case it's important this is compiling with FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for win64 (64bit)
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Re: FB OOP Inheritence

Postby MystikShadows » Dec 17, 2019 23:59

fxm wrote:I assume that your 2 modules are compiled as 2 separate files (and not one included in the other).
So, only the declarations of the base type (Type BaseItem ... End Type) must be duplicated in the two modules, not the definitions of the procedures.

To the best of my knowledge no. BaseItemClass.bi in one module and is both class definition and function implementation. same thing for ContactItemClass.bi has the definition and implementation of the ContactItemClass. ContactItemClass.bi #Include Once "BaseItemClass.bi" if it's in the code, it tells me duplicate definition of BaseItemClass, if not, if there is no #Include Once "BaseItemClass.bi" then it tells me expected identifier, BaseItemClass found. like instead.
paul doe
Posts: 1255
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: FB OOP Inheritence

Postby paul doe » Dec 18, 2019 0:48

@MystikShadows: why don't you try with a header guard, like this?

Code: Select all

#ifndef __WHATEVER_BASEITEMCLASS__
#define __WHATEVER_BASEITEMCLASS__

/'
  Code for the BaseItemClass here
'/

#endif
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Re: FB OOP Inheritence

Postby MystikShadows » Dec 18, 2019 2:12

paul doe wrote:@MystikShadows: why don't you try with a header guard, like this?

Code: Select all

#ifndef __WHATEVER_BASEITEMCLASS__
#define __WHATEVER_BASEITEMCLASS__

/'
  Code for the BaseItemClass here
'/

#endif

I get the same results.
Xusinboy Bekchanov
Posts: 243
Joined: Jul 26, 2018 18:28

Re: FB OOP Inheritence

Postby Xusinboy Bekchanov » Dec 18, 2019 3:36

What error does it give? It compiles normally for me.
fxm
Posts: 9820
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FB OOP Inheritence

Postby fxm » Dec 18, 2019 6:00

I do not understand.
The example you provided above compiles OK when compiling the "ContactItemClass" file (do not compile both because 'BaseItemClass' is already included in "ContactItemClass").
Imortis
Moderator
Posts: 1725
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FB OOP Inheritence

Postby Imortis » Dec 18, 2019 17:18

I have found that if the implementation for the constructor and destructor is not in the code, LD will exit with error code 1. It crashes. Would that be an error on the FB side, or in the linker?

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests