FB OOP Inheritence

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

FB OOP Inheritence

Post by MystikShadows »

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: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FB OOP Inheritence

Post by Imortis »

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

Post by MystikShadows »

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: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FB OOP Inheritence

Post by Imortis »

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
Moderator
Posts: 12110
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: FB OOP Inheritence

Post by fxm »

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: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: FB OOP Inheritence

Post by badidea »

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

Post by MystikShadows »

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

Post by MystikShadows »

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
Moderator
Posts: 1733
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: FB OOP Inheritence

Post by paul doe »

@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

Post by MystikShadows »

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: 789
Joined: Jul 26, 2018 18:28

Re: FB OOP Inheritence

Post by Xusinboy Bekchanov »

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

Re: FB OOP Inheritence

Post by fxm »

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: 1924
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: FB OOP Inheritence

Post by Imortis »

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?
Post Reply