Repository navigation
Class
Home › Interfaces › Class
Declares a type that always extends object: it has a vtable, run-time type information, and may implement interfaces.
class name [ ( of T [ , ... ] ) ] [ field = alignment ] [ alias "mangled" ]
[ inherits base_class ]
[ implements interface [ , interface ... ] ]
member declarations, fields, access labels ...
end class-
name: the class's name. -
T: a type parameter, making a generic class. -
base_class: the class it derives from: aclass, or atypethat extendsobject. Without aninheritsline the base isobject. -
interface: an interface the class implements. SeeImplements.
A class is a Type written so that the object model is the default. class Person … end class declares the same thing as type Person extends object … end type: every object carries a pointer to its vtable, virtual, abstract and override work in it, and Operator Is can test it.
The base and the interfaces are lines at the top of the body, not words on the header:
-
inherits base_classnames the one base class (seeInherits). A secondinheritsline, or two names on one, is an error (TKO0427), and so isextendson a class's header (TKO0432). -
implements …lines list the interfaces. Any number of interfaces, on one line or several.
inherits comes before implements, and both come before any member (TKO0435). Everything else in the body is written as in a type: fields, declare lines for constructors, methods, properties and operators, and private: / protected: / public: labels. Method bodies are written outside, as sub Person.Greet( ).
interface IGreeter
sub Greet( )
end interface
class Person '' extends object: it has a vtable
implements IGreeter
declare constructor( byref nm as ustring = "nobody" )
declare virtual sub Greet( ) implements IGreeter.Greet
as ustring nm
end class
constructor Person( byref nm as ustring = "nobody" )
this.nm = nm
end constructor
sub Person.Greet( )
print "hello, I am "; nm
end sub
class Pirate
inherits Person '' the base class, on its own line
declare constructor( byref nm as ustring )
declare sub Greet( ) override
end class
constructor Pirate( byref nm as ustring )
base( nm )
end constructor
sub Pirate.Greet( )
print "arr, "; nm; " be me name"
end sub
dim as Person ann = Person( "Ann" )
dim as Pirate bob = Pirate( "Bob" )
dim as IGreeter g = ann
g.Greet( )
g = bob '' a Pirate is a Person, so it is an IGreeter
g.Greet( )
dim as Person ptr p = @bob
print "p points at a Pirate: "; cbool( *p is Pirate )hello, I am Ann
arr, Bob be me name
p points at a Pirate: true
A class and a type mix freely: a class may inherit a type that extends object, and a type may extend a class.
- A class cannot be
extern "C"orextern "C++"(TKO0437): its layout has a vtable pointer that C cannot describe. -
classmakes no member virtual by itself. Writevirtualwhere a derived class should be able to replace a method, and repeat it on anoverridethat should itself be overridable. - A plain
typecan implement interfaces too, without a vtable. Chooseclasswhen objects need inheritance or run-time type tests, andtypefor small values.
Category: Interfaces Kind: keyword
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing