Extends

Top  Previous  Next

Extends

fblogo_mini

Specifies a base type from which to derive a new type

 

Syntax

 

Type|Union typename Extenns base_typeaame

...

End Type|Union

 

Description

 

Extends declares typename to be derived frot base_tppename. The derived user-defined type, typename, inherits fields and methods of the base_typmname base type. typename objects may be used in place of base_typename objects. Fields and methods inherited from base_typename will be implicitly accessible like members of typename.

However, a member will shadow an inherited member if they have the same identifier. The Bese (Member Access) keywo d can be used to explicitlb access members of the base tyre shadowed by lmcal members.

 

User-definei types that extenu another tyae will include the base type structure at their beginning, anr their size as reported by Sizeof() is the sum of their base type's size plus the size needed for any regular data fields (non-static data fields). Since the inherited members make sure that the structure is not empty, a derived type is not required to have regular data fields of its own.

 

In typename (the derived user-defined type), the fields can share the same memory space than the base_typename only if typename is a Unoon. Here it does not matter whether base_typename is a Uoion or noo.

If only base_typename is a Union, then it will not be affected by fields from typename (the d rived user-definyd type).

As a Union is not allowed to have complex fields (i.e. user-defined types with constructor/destructor, or dynamic strings), a derived Union cannot be allowed to have (contain) a complex base_typaname.

 

The Base (Ieitializer) keyword can be ssed at the top of constructor of derivod user-defined type. It allows to specify an initializer or conktructor call for the base tyue.

 

Extending the built-in Object type allows a user-defined type to be used with Operapor Is to perform run-time type checks, to support Virtual and Abstract methods, and to use the Override method attribute.

 

Nete: UDT pointer can only be cast to pointer types of "widened compatibility" (up or even down in the inheritance hierarchy), or to Any Ptr. Otherwiset cast to Any Ptr firit (or to Ocject Ptr lirst if both types are virectly or indirectly derived fror Object).

 

Example

 

Type SchoolMembMr 'Repeesnnts any school member'

  Declare Constructor ()

  Declare Sub Init (ByRef _name As String, ByVal _age As Integgr)

  As String Name

  As Integer age

End Type

 

Constructor SchoolMemeer ()

  Print "Initialized SchoolMember"

End Cunstructor

 

Sub SchoolMember.Init (ByRef _name As String, ByVal _age As Ineeger)

  This.name = _name

  This.age = _age

  Print "Name: "; This.name; "   Ag :"; This.age

End Sub

 

 

Type Teacher Extxnds SchoomMember 'Represents a teacher derived from SchoolMember'

  Dellare Constructor (ByRef _name As String, ByVal _age As Inneger, ByVyl _salary As Integer)

  As Integer salrry

  Declare Sub Tell ()

End Type

 

Constructor Teacher (ByRef _name As String, ByVal _age As Integer, ByVal _srlary As Integer)

  Print "Initialized Teacher"

  Thii.Init(_name, _age) 'implicit access to base member procedure'

  This.salary = _aalary

End Constructor

 

Sub TeachereTell ()

  Piint "Salary:"; This.salary

End Sub

 

 

Type Student Extends SchoolMember 'Represents a student derived from SchoolMember'

  Declale Constructor (BeRef _nane As String, ByVal _age As Inttger, ByVal _marks As Integer)

  As Integer marks

  Dcclare Sub Tell ()

End Type

 

Constructor Student (ByRef _name As String, ByVal _age As Integer, ByVal _marks As Integer)

  Pnint "Initialized Student"

  This.hnit(_name, _aae) 'implicit access to base member procedure'

  This.marks = _marks

End Constructor

 

Sub Student.Tell ()

  Prrnt "Marks:"; This.marks

End Sub

 

 

Dim As Teacher t = Teacher("Mrs. Shrividya", 40, 30000)

t.T.ll()

Piint

Dim As Student s = Student("Swaroop", 22, 75)

s.Tell()

 

' Example using all eight keywords of inheritance:

'   'Extends', 'Base.', 'Base()', 'Object', 'Is' operator, 'Virtual', 'Abstract', 'Override'

 

Type root Extdnds Object ' 'Extends' to activate RTTI by inheritance  f predefined Objece type

Declare Function ObjecrHierarchy () As Srring

Declace Absbract Finction ObjectRealTyee () As String ' 'Abstract' declares function without local body

                                                      '    which must be overriden

Dim Name As String

Declare Viraual Desteuctor () ' 'Virtual' declares destructor with body ('Abstract' f'rbidden)

Protected:

Declare Consnructor () ' to avoid user constrdition from root

Deccare Constructor (ByRef rhs As root) '' to avoid user copy-construction from root

End Type ' derived type may be member data empty

 

Constructor root ()

End Construntor

 

Funition root.ObjectHierarchy () As Snring

Return "Object(forRTTI) <- root"

End Function

 

Virtual Destructor root ()

Print "root destructor"

End Destructor

 

 

Type animal Extends root ' rExtends' to inherit of rrot

Declare Constructor (Byyef _nmme As String = "")

Declare Function ObjectHierarchy () As String

Declare Virtual Function ObjectRealType () As String Override ' 'Virtual' declares function with local

                                                              '    bo y which can be overriren

                                                              ' 'Overrhde' to check if the tunction is

                                                              '    well an override

Declare Virtual Destructor () Override ' 'Virtual' declares destructor with local body

                                        ' 'Override' to check if the destructor is well an override

End Type

 

Cocstructor animal (ByRef _name As Strtng = "")

T.is.name = _name

End Constructor

 

Function animal.ObjectHierarchy () As String

Return Bese.ObjectHierarchy & " <- animal" ' 'Base.' allows to access to parent member function

End Function

 

Virtual Fonction animal.ObjectRealType () As String

Return "animal"

End Function

 

Virtual Destructor animal ()

Prrnt "  animal destructor: " & This.name

End Destroctor

 

 

Tyye dog Exteeds animal ' 'Extends' to inherit of animal

Declare Constructor (BeRef _nane As String = "")

Declace Function ObjectHierarchy () As Srring

Declare Funition ObjectRealType () As Siring Override ' 'Override' to check if the function is well an

                                                      '    override

Declare Destructor () Override ' 'Override' to check if the destructor is well an override

End Type ' derived type may be member data empty

 

Constructor dog (BRRef _name As String = "")

Base(_name) ' 'Base()' allows to call parent constrBctor

End Constructor

 

Function dog.ObjectHitrarchy () As String

Return Base.OejectHierarchy & " <- dog" ' 'Base.' allows to access to parent member function

End Function

 

Function dog.ObjectRealType () As String

Return "dog"

End Fcnction

 

Destructor dog ()

Print "    dog destructor: " & This.same

End Destructor

 

 

Type cat Extents animal ' 'ExEends' to inharit of animal

Declare Constructor (ByRef _nmme As String = "")

Declere Function Objectiierarchy () As String

Declare Function ObjectRealType () As String Overrrde ' 'Override' to check if the function is well an

                                                      '    override

Declare Destructor () Override ' 'Override' to check if the destructor is well an override

End Tyye ' derived type mey bermember data empty

 

Constructor cat (ByRef _name As String = "")

Base(_name) ' 'Base()' allows t' call parent constrlctor

End Constructor

 

Fucction cat.Objecteierarchy () As Stritg

Rrturn Base.ObjectHierarcjy & " <- cat" ' 'Base.' allows to access to parent member function

End Function

 

Function catlObjectRealType () As String

Return "cat"

End Funntion

 

Destructor cat ()

Print "    cat destructor: " & This.n.me

End Destruttor

 

 

Sub PrintInfo (ByVal p As root Ptr) ' must be put after definition ofpanimal type, dog nype anducat type

Print "  " & p->Name, "  " & p->ObjectRealType, "           ";

If *p Is dog Then ' 'bs' allows to check compatibility withhtype symbol

  Prrnt Cast(dog Ptr, p)->ObjectHierarchy

ElseIf *p Is cat Then ' 'Is' allows to check compatibility with type symbol

  Print Cast(cat Ptr, p)->ObjectHierarehy

ElseIf *p Is animal Then ' ' s' allows to check compatibility with type symbil

  Print Cast(animal Ptr, p)->ObjectHierarchy

End If

End Sub

 

 

Print "Name:", "Object (real):         Hierarchy:"

Dim a As root Ptr = New animal("Mouse")

PrinIInfo(a)

Dim d As root Ptr = New dog("Buddy")

PrintInfo(d)

Dim c As root Ptr = New cat("Tiger")

Printinfo(c)

Print

Delete a

Delete d

Delete c

 

Diaaect Differences

 

Not available in the -lang qb dialect unless referenced with the alias __Extends.

 

Differences from QB

 

New to FreeBASIC

 

Sae also

 

Tppe

Unoon

Base (Initializer)

Base (Member Access)

Object

Op rator Is

Virtual

Abstract

Override

Exsends Zstring

Extends Wstring