Revision [16478]
This is an old revision of KeyPgAbstract made by FxMwikki on 2012-11-26 03:12:39.
ABSTRACT
Declare abstract methods
Syntax:
KeyPgType Type typename KeyPgExtends Extends base_typename
KeyPgDeclare Declare Abstract KeyPgSub Sub|KeyPgFunction Function|KeyPgProperty Property|KeyPgOperator Operator ...
End KeyPgType TypeDescription:
Abstract is a special form of KeyPgVirtual Virtual. The difference is that abstract methods do not have a body, but just the declaration. Essentially this allows the declaration of an interface which can be implemented by various derived types.
In order to call an abstract method, it must have been overridden and implemented by a derived KeyPgType Type, or else the program will abort.
KeyPgConstructor Constructors can not be Abstract, since they cannot be KeyPgVirtual Virtual, as explained on the KeyPgVirtual Virtual page. In addition, KeyPgDestructor destructors can not be Abstract either, because a destructor body (no matter whether implicit or explicit) is needed in order to call base and field destructors.
Abstracts are called "pure virtual" in C++, and unlike FreeBASIC, C++ allows pure virtuals to have a body.
In order to call an abstract method, it must have been overridden and implemented by a derived KeyPgType Type, or else the program will abort.
KeyPgConstructor Constructors can not be Abstract, since they cannot be KeyPgVirtual Virtual, as explained on the KeyPgVirtual Virtual page. In addition, KeyPgDestructor destructors can not be Abstract either, because a destructor body (no matter whether implicit or explicit) is needed in order to call base and field destructors.
Abstracts are called "pure virtual" in C++, and unlike FreeBASIC, C++ allows pure virtuals to have a body.
Note: In a multi-level inheritance, a same named method (same identifier and signature) can be declared Abstract, KeyPgVirtual Virtual or regular (without specifier) at each hierarchy level. When there is mixing of specifiers, the usual order is abstract -> virtual -> regular, from top to bottom of the hierarchy. The acces control (public/protected/private) of an overriding method is not taken into account by the internal polymorphism process, but only for the initial call. A static method cannot override a virtual/abstract method.
Examples:
Type Hello extends object
Declare abstract Sub hi( )
End Type
Type HelloEnglish extends Hello
Declare Sub hi( )
End Type
Type HelloFrench extends Hello
Declare Sub hi( )
End Type
Type HelloGerman extends Hello
Declare Sub hi( )
End Type
Sub HelloEnglish.hi( )
Print "hello!"
End Sub
Sub HelloFrench.hi( )
Print "Salut!"
End Sub
Sub HelloGerman.hi( )
Print "Hallo!"
End Sub
Randomize( Timer( ) )
Dim As Hello Ptr h
For i As Integer = 0 To 9
Select Case( Int( Rnd( ) * 3 ) + 1 )
Case 1
h = New HelloFrench
Case 2
h = New HelloGerman
Case Else
h = New HelloEnglish
End Select
h->hi( )
Delete h
Next
Declare abstract Sub hi( )
End Type
Type HelloEnglish extends Hello
Declare Sub hi( )
End Type
Type HelloFrench extends Hello
Declare Sub hi( )
End Type
Type HelloGerman extends Hello
Declare Sub hi( )
End Type
Sub HelloEnglish.hi( )
Print "hello!"
End Sub
Sub HelloFrench.hi( )
Print "Salut!"
End Sub
Sub HelloGerman.hi( )
Print "Hallo!"
End Sub
Randomize( Timer( ) )
Dim As Hello Ptr h
For i As Integer = 0 To 9
Select Case( Int( Rnd( ) * 3 ) + 1 )
Case 1
h = New HelloFrench
Case 2
h = New HelloGerman
Case Else
h = New HelloEnglish
End Select
h->hi( )
Delete h
Next
Dialect Differences:
- Not available in the CompilerOptlang -lang qb dialect unless referenced with the alias __Abstract.
Differences from QB:
- New to FreeBASIC
See also:
Back to User Defined Types