xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

CLASS

Declares the class function of a user-defined class.

Syntax

CLASS <ClassName> [ FROM <SuperClass,...> ] [ STATIC ]
  [EXPORTED: | PROTECTED: | HIDDEN:]
  <Member declarations>
  <...>
ENDCLASS

Arguments

<ClassName>
This is the symbolic name of the class to declare. It must begin with a letter or underscore followed by digits, letters or underscores. The symbolic name can contain up to 63 characters.
FROM <SuperClass,...>
The FROM option specifies one or more classes the new class is derived from. The names of these classes must be comma separated and must exist in an application. <ClassName> inherits all member variables and methods from its super class(es). A synonym for FROM is INHERIT (CLASS <ClassName> INHERIT <SuperClass,...>)
STATIC
When this option is specified, the class function is created as STATIC FUNCTION. In this case, objects of the class can only be created in the PRG module declaring the class.
EXPORTED: | PROTECTED: | HIDDEN:
The EXPORTED:, PROTECTED: and HIDDEN: options modify the visibility attribute for the member variables and methods of the class. The default attribute is EXPORTED:. See an explanation for the visibility attribute in the description.

Note that the colon is mandatory at the end of this option. If it is missing, the compiler reports a syntax error.

<Member declarations>
Member variables and methods of the class must be declared separately using the statements DATA and METHOD. These declarations define the symbolic names for accessing member variables and executing methods for objects of a class.
ENDCLASS
This terminates the class declaration and is mandatory. If the CLASS declaration does not end with ENDCLASS, the compiler reports a syntax error.

Description

The CLASS statement declares the Class function of a user-defined class. This function is required to instatiate a class, i.e. to create objects of a class. Objects are always created with the :new() method, which is part of the abstract base class HBObject() of xHarbour's OOP model.

The CLASS declaration ends with ENDCLASS. Between CLASS and ENDCLASS, the symbolic names for the members of a class must be declared with DATA (instance variables) and/or METHOD (instance methods) and/or CLASSDATA (class variables) and/or CLASSMETHOD (class methods). When the class declaration is complete, declared methods must be implemented with another METHOD declaration. It is an error when a method is declared but not implemented, and vice versa.

Inheritance

A new class can be declared on the basis of existing classes. The name of one or more classes is specified with the FROM option. In this case, the new declared class inherits all members of <SuperClass,...> and can access them unless their visibility attribute is set to HIDDEN:.

If no superclass is specified, HBObject() is used as implicit superclass, i.e. all methods of HBObject() exist in all user-defined classes.

Visibility of members

Access to member variables and methods of an object can be restricted by their visibility attribute, also known as Scope. When an object is used outside the PRG module declaring the class, the following rules apply for the visibility attribute:

EXPORTED: Access to member variables and methods with this attribute is not restricted. They can be accessed in any context, including FUNCTION and PROCEDURE.

PROTECTED: Protected member variables and methods are accessible in the class(es) declared in the PRG module and all subclasses. Subclasses are all classes that inherit directly or indirectly from the declared class. A protected member is accessible within the context of a METHOD implemented in the declaring PRG module or in the PRG module of a subclass. Protected members are not accessible within the context of FUNCTION and PROCEDURE.

HIDDEN: This attribute restricts access to member variables and methods to the methods of the class(es) declared in the PRG module. It is the most restrictive attribute. Hidden members cannot be accessed in the context of a FUNCTION or PROCEDURE, only in the context of a METHOD implemented in the declaring PRG module.

Info

See also:ACCESS, ASSIGN, DATA, EXPORTED:, HBObject(), HIDDEN:, METHOD (declaration), PROTECTED:, VAR
Category: Class declaration , Declaration , xHarbour extensions
Header:hbclass.ch
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates accessibility of member variable using a
// base class, a subclass and a PROCEDURE. Note that the example consists of
// four files: MAIN.PRG, PRINT_PROC.PRG, BASECLASS.PRG and SUBCLASS.PRG.
// The output of the example is given at the end of the listings.

   ************************* MAIN.PRG ****************************
   PROCEDURE Main
      LOCAL obj

      obj := BaseClass():new()

      ? "==================="
      ? "Testing base class"
      ? "==================="
      Print_Proc( obj )            // prints in a PROCEDURE
      obj:print()                  // prints in a METHOD

      obj := SubClass():new()

      ? "================"
      ? "Testing subclass"
      ? "================"
      Print_Proc( obj )            // prints in a PROCEDURE
      obj:print()                  // prints in an overloaded METHOD
      obj:super:print()            // prints in the original METHOD

   RETURN

   ********************** PRINT_PROC.PRG *************************
   // prints the contents of an object's instance variables
   PROCEDURE Print_Proc( o )
      LOCAL oErr
      ?
      ? "---", ProcName() , "---"

      TRY
        ? "EXPORTED:  "
        ?? o:iExported
      CATCH oErr
        ?? oErr:description
      END

      TRY
        ? "PROTECTED: "
        ?? o:iProtected
      CATCH oErr
        ?? oErr:description
      END

      TRY
        ?  "HIDDEN:   "
        ?? o:iHidden
      CATCH oErr
        ?? oErr:description
      END
   RETURN


   ********************** BASECLASS.PRG **************************
   // prints the contents of instance variables in a method
   #include "Hbclass.ch"

   CLASS BaseClass
      HIDDEN:
      VAR iHidden    INIT "iHidden"

      PROTECTED:
      VAR iProtected INIT "iProtected"

      EXPORTED:
      VAR iExported  INIT "iExported"

      METHOD print
   ENDCLASS


   METHOD Print CLASS BaseClass
      LOCAL oErr
      ?
      ? "---", ProcName() , "---"

      TRY
        ? "EXPORTED:  "
        ?? ::iExported
      CATCH oErr
        ?? oErr:description
      END

      TRY
        ? "PROTECTED: "
        ?? ::iProtected
      CATCH oErr
        ?? oErr:description
      END

      TRY
        ?  "HIDDEN:   "
        ?? ::iHidden
      CATCH oErr
        ?? oErr:description
      END
   RETURN self


   **********************  SUBCLASS.PRG **************************
   // prints the contents of instance variables in an overloaded
   // method
   #include "Hbclass.ch"

   CLASS SubClass FROM BaseClass
      METHOD print
   ENDCLASS


   METHOD Print CLASS SubClass
      LOCAL oErr
      ?
      ? "---", ProcName() , "---"

      TRY
        ? "EXPORTED:  "
        ?? ::iExported
      CATCH oErr
        ?? oErr:description
      END

      TRY
        ? "PROTECTED: "
        ?? ::iProtected
      CATCH oErr
        ?? oErr:description
      END

      TRY
        ?  "HIDDEN:   "
        ?? ::iHidden
      CATCH oErr
        ?? oErr:description
      END
   RETURN self

   ************************** Output *****************************
   ===================
   Testing base class
   ===================

   --- PRINT_PROC ---
   EXPORTED:  iExported
   PROTECTED: Scope Violation <PROTECTED>
   HIDDEN:    Scope Violation <HIDDEN>

   --- BASECLASS:PRINT ---
   EXPORTED:  iExported
   PROTECTED: iProtected
   HIDDEN:    iHidden

   ================
   Testing subclass
   ================

   --- PRINT_PROC ---
   EXPORTED:  iExported
   PROTECTED: Scope Violation <PROTECTED>
   HIDDEN:    Scope Violation <HIDDEN>

   --- SUBCLASS:PRINT ---
   EXPORTED:  iExported
   PROTECTED: iProtected
   HIDDEN:    Scope Violation <HIDDEN>

   --- BASECLASS:PRINT ---
   EXPORTED:  iExported
   PROTECTED: iProtected
   HIDDEN:    iHidden

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe