Navigation  without Java Scripts

Class Declaration

Syntax

CLASS <class_name> [: <base_class_list>]
{[PROTECTED] DOMAINS <domains_declarations>}
{[STATIC] [PROTECTED] PREDICATES <predicates_declarations>}
{[STATIC] [PROTECTED] FACTS [- <facts_section_name>] <facts_declarations>}
ENDCLASS [<class_name>]

Syntax of Abstract Class Declaration

ABSTRACT CLASS <class_name> [: <base_class_list>]
{[PROTECTED] DOMAINS <domains_declarations>}
{[PROTECTED] PREDICATES <predicates_declarations>}
ENDCLASS [<class_name>]

A class has to be declared before it can be referenced in the program code.

CLASS keyword

The class declaration section begins with the keyword CLASS. The CLASS keyword should be followed by the class header that includes the class name and an optional list of base (parent) classes.

If it is necessary to declare an abstract class, then the keyword ABSTRACT should be specified before the keyword CLASS.
< class_name >
The class_name specifies the name of the class. The class name should be unique within a project. The declaration of a class with a name class_name automatically generates a global domain class_name. The generated class_name domain can be used to declare arguments for predicates that should handle references to the objects of this class.
<base_class_list>

The base class list includes a base (parent) class or classes from which the class class_name will derive (or inherit) predicates and facts. If any base class is specified, the class class_name is called a derived class. Each class can be met in this list only once. The class name cannot be specified as the base class for itself.
For example:

CLASS child : parent1, parent2

Domains, Predicates, and Facts Sections in the Class Declaration

Domains, predicates, and facts sections can be declared inside a class declaration.

In abstract classes only domains and predicates sections can be declared.

Several domains, predicates, and facts sections can be declared in a class declaration.

The class members declared in the class declaration have the public default access rights.

Class members declared in the class implementation are private.

Each class, except abstract classes, must have both the class declaration and the class implementation that follows the class declaration.

An abstract class is used to provide general concepts (interfaces). The purpose of an abstract class is to have a declaration of some virtual predicates in order to have some other predicates working on a different specialization of the more general class. An abstract class can be used only as a base class for other classes. No objects of an abstract class can be created.

Beginning with Visual Prolog version 5.2, Visual Prolog's class system is much more compatible with COM. Abstract class provides exactly the same VTABLE as the COM interface.

Example

CLASS XString
PREDICATES
writeString
ENDCLASS
IMPLEMENT XString
FACTS
determ strDB(string)
CLAUSES
writeString :-
assert(strDB("Hello world\n")),
strDB(X),
write(X),
retractall(strDB(_)).
ENDCLASS XString
GOAL
NewObject = XString::new,
NewObject:writeString,
NewObject:delete.
Caution! Our position is that using of public (global) facts is a dangerous practice and is not a good programming style. Therefore, we recommend never to declare facts in class declarations. We recommend always to encapsulate class facts in class implementation and provide access to them only with class predicates (methods). According to our plans, future versions of Visual Prolog will not support public facts.
See also