FB OOP Inheritence
-
- Posts: 612
- Joined: Jun 15, 2005 13:22
- Location: Upstate NY
- Contact:
FB OOP Inheritence
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. :)
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. :)
Re: FB OOP Inheritence
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
-
- Posts: 612
- Joined: Jun 15, 2005 13:22
- Location: Upstate NY
- Contact:
Re: FB OOP Inheritence
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.
Re: FB OOP Inheritence
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?
Re: FB OOP Inheritence
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.
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.
Re: FB OOP Inheritence
Try "Base1" (or some other name) instead "Base". Code of Imortis above is not 'real code'.
-
- Posts: 612
- Joined: Jun 15, 2005 13:22
- Location: Upstate NY
- Contact:
Re: FB OOP Inheritence
BaseItemClass:
ContactItemClass:
And just in case it's important this is compiling with FreeBASIC Compiler - Version 1.07.1 (2019-09-27), built for win64 (64bit)
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
-
- Posts: 612
- Joined: Jun 15, 2005 13:22
- Location: Upstate NY
- Contact:
Re: FB OOP Inheritence
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.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.
Re: FB OOP Inheritence
@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
-
- Posts: 612
- Joined: Jun 15, 2005 13:22
- Location: Upstate NY
- Contact:
Re: FB OOP Inheritence
I get the same results.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
-
- Posts: 789
- Joined: Jul 26, 2018 18:28
Re: FB OOP Inheritence
What error does it give? It compiles normally for me.
Re: FB OOP Inheritence
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").
The example you provided above compiles OK when compiling the "ContactItemClass" file (do not compile both because 'BaseItemClass' is already included in "ContactItemClass").
Re: FB OOP Inheritence
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?