Skip to content

Classes

Classes are templates used to define custom object types.

A class can contain variables and functions.


Syntax

Use the class keyword to declare a class.

class MyClass {
}

Class Variables

Variables declared inside a class belong to every instance created from it.

class MyClass {
    Int x : 100;
}

In this example:

  • x is a class variable
  • its default value is 100

Class Functions

Functions can also be declared inside classes.

class MyClass {
    Int x : 100;

    func execute(){
        DisplayLog(x);
    }
}

Functions inside a class can access its variables directly.


Constructor

A constructor is a function automatically called when an object is created.

In CES, the constructor is declared using _main_.

class MyClass {
    func _main_(){
        DisplayLog("object created");
    }
}

When an instance is created:

Inst obj : MyClass();

The _main_ function runs automatically.

Output:

object created

Note

The _main_ function is optional.
If not declared, the object will still be created normally.

Constructor Parameters

The _main_ function can receive parameters when an object is created.

class Person {
    Str name;

    func _main_(Str n){
        name = n;
    }
}

Creating an instance:

Inst user : Person("Thiago");
DisplayLog(user.name);

Output:

Thiago

Restrictions

Warning

Functions declared inside a class cannot use the global modifier.

Invalid example:

class MyClass {
    func global myfunction(){
    }
}

Class functions are already part of the object instance and should be declared only with func.


Complete Example

class myclass {
    Int x : 100;

    func execute(){
        DisplayLog("hello world " + x);
    }
}

Notes

Note

A class only defines the structure.
To use it, create an instance with Inst.