| xHarbour Reference Documentation > Statement Reference |
![]() |
![]() |
![]() |
Declares the class function of a user-defined class.
CLASS <ClassName> [ FROM <SuperClass,...> ] [ STATIC ] [EXPORTED: | PROTECTED: | HIDDEN:] <Member declarations> <...> ENDCLASS
Note that the colon is mandatory at the end of this option. If it is missing, the compiler reports a syntax error.
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. |
| 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 |
// 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
http://www.xHarbour.com