Main Index : Reference :

class


Description

Declares a class.

Usage

class class_name : [class] parent_class
{
private:
  var private_variable;
  private_member_function(...);
protected:
  var protected_variable;
  protected_member_function(...);
public:
  var public_variable;
  public_member_function(...);
  
  class_name(...); // Constructor
  finalize(); // Destructor
}

class_name::class_name(...)
{
  // Constructor
}

class_name::member_function(...)
{
  // Do something
}

Declaration

class declares a class named class_name, optionally derived from the base class parent_class. The member variables and function prototypes are declared inside the following block. Function implementations are declared afterwards, using the notation class_name::function_name(...). They cannot be written inline. Classes can only be declared globally.

Constructors have the same name as the class. Contrary to in C++ they can return a value, but only NULL or a this reference is sensible (used to report either success or failure). If no constructor is specified, a default constructor is generated that returns this. Parent constructors should be called using super().

Destructors are always named finalize(). Because of the garbage collection in C.O.F.F.E.E. they are rarely needed. A default destructor is automatically generated. When and in what order destructors are called is not specified.

Access privileges are specified with the private:, protected: and public: labels. Public functions and variables are accessible from any function. Protected functions and variables are accessible from member functions of the class or from member functions of derived classes. Private functions and variables are only accessible from member functions of the class, not from derived classes. Each label can appear many times in the class declaration. The default level of access in a class is private:.

Label All functions Member functions Derived functions
public: accessible accessible accessible
protected: hidden accessible accessible
private: hidden accessible hidden

Example

class myClass
{
private:
  var x,y;
public:
  myClass(a,b); // Constructor
  
  GetX();
  GetY();
}

myClass::myClass(a,b)
{
  // Return NULL if the declaration fails
  if (a<b) return NULL;
  
  x = a;
  y = b;
}

myClass::GetX()
{
  return x;
}

myClass::GetY()
{
  return y;
}

function()
{
  var obj = new(myClass,5,4);
  
  // Outputs "5"
  println(obj->GetX());
  
  // Error. Cannot access private variables directly!
  println(obj->x);
}


// An example of a derived class and how the access 
// privileges are inherited in derived classes:

class myBase
{
private:
  var a;
protected:
  var b;
public:
  var c;
  
  myBase(_a, _b, _c); // Constructor
}

myBase::myBase(_a, _b, _c)
{
  a = _a;
  b = _b;
  c = _c;
}

class myClass : myBase
{
public:
  myClass(x, y, z); // Constructor
}

myClass::myClass(x, y, z)
{
  super(x, y, z); // Call the parent's constructor
  
  // Error. Derived classes cannot access private 
  // variables in the base class!
  println(a);
  
  // OK. Protected variables are accessible in
  // member functions of derived classes.
  println(b);
}

function()
{
  var obj = new(myClass,1,2,3);
  
  // Error. Cannot access private or protected variables!
  println(obj->a);
  println(obj->b);
  
  // OK. Public variables can be accessed everywhere.
  println(obj->c);
}

See Also

new(), var, super(), getclass(), getparent()