Quick start manual

7-2
Delphi Language Guide
Class types
Class types
A class type must be declared and given a name before it can be instantiated. (You
cannot define a class type within a variable declaration.) Declare classes only in the
outermost scope of a program or unit, not in a procedure or function declaration.
A class type declaration has the form
type className = class (ancestorClass)
memberList
end;
where className is any valid identifier, (ancestorClass) is optional, and memberList
declares members—that is, fields, methods, and properties—of the class. If you omit
(ancestorClass), then the new class inherits directly from the predefined TObject class.
If you include (ancestorClass) and memberList is empty, you can omit end. A class type
declaration can also include a list of interfaces implemented by the class; see
“Implementing interfaces” on page 10-4.
Methods appear in a class declaration as function or procedure headings, with no
body. Defining declarations for each method occur elsewhere in the program.
For example, here is the declaration of the TMemoryStream class from the Classes unit.
type
TMemoryStream = class(TCustomMemoryStream)
private
FCapacity: Longint;
procedure SetCapacity(NewCapacity: Longint);
protected
function Realloc(var NewCapacity: Longint): Pointer; virtual;
property Capacity: Longint read FCapacity write SetCapacity;
public
destructor Destroy; override;
procedure Clear;
procedure LoadFromStream(Stream: TStream);
procedure LoadFromFile(const FileName: string);
procedure SetSize(NewSize: Longint); override;
function Write(const Buffer; Count: Longint): Longint; override;
end;
TMemoryStream descends from TStream (in the Classes unit), inheriting most of its
members. But it defines—or redefines—several methods and properties, including its
destructor method, Destroy. Its constructor, Create, is inherited without change from
TObject, and so is not redeclared. Each member is declared as private, protected, or
public (this class has no published members); for explanations of these terms, see
“Visibility of class members” on page 7-4.
Given this declaration, you can create an instance of TMemoryStream as follows:
var stream: TMemoryStream;
stream := TMemoryStream.Create;