Navigation  without Java Scripts

Classes and Objects in Visual Prolog

Visual Prolog contains a powerful object mechanism, that combines the Logic programming and object oriented programming (OOP) paradigms.

Class Definition in Visual Prolog

Defining a class in Visual Prolog requires in general two things: the class declaration, and the class implementation.

The class declaration specifies the interface to a class; that is, domains, predicates, and facts that can be seen from the outside of the class.

The class implementation contains Prolog clauses (and other sections) defining the actual functionality of a class.

That is, the declaration of the class interface and the definition of the class functionality are separated. A class declaration (interface) can often be placed in a header file that should be included in those modules of the project, which use members of this class. A class implementation can be placed in any project module, which includes the class declaration.

In Visual Prolog declaration of a class with the name class_name automatically generates an internal global -domain class_name. The generated class_name domain can be used as an ordinary global domain to declare arguments for predicates that should handle references to objects of this class. Most rules applied to ordinary global domains are applied to the class domains.

Each class, except abstract classes, must have both the declaration and the implementation. Abstract classes cannot have implementations.

<class > ::= <class_declaration> <class_implementation>

<abstract_class > ::= ABSTRACT <class_declaration>

Class Members in Visual Prolog

Domains, predicates, and facts declared in a class (both in the class declaration and in the class implementation) are called class members: member-domains, member-predicates, and member-facts.

Class members' default access rights are determined by the place where the class member is declared (in the class declaration or in the class implementation).

Classes as Modules

Beginning with version 5.2 Visual Prolog provides possibility to declare domains inside classes. Being able to declare domains in classes opens the possibility to use classes as modules.

Public domains declared in a class declaration are global and can be used outside the class. All domains declared in classes are static/class entities. The domain belongs to the class not to the individual objects.

If a class declares only static/class entities, then it can be considered a module.

The static/class entities of a class can be used as ordinary global entities, as long as you remember to qualify them with the class name. One advantage of creating modules this way is that the module will have a separate name space (as the result of qualification with the class name). This means that you can choose names in the module more freely. It also ensures consistent naming of all entities in the module. Another advantage is that classes do not have to be included in the main module, even if they contain public domains.

See also