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 Variables
Variables declared inside a class belong to every instance created from it.
In this example:
xis a class variable- its default value is
100
Class Functions
Functions can also be declared inside classes.
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_.
When an instance is created:
The _main_ function runs automatically.
Output:
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.
Creating an instance:
Output:
Restrictions
Warning
Functions declared inside a class cannot use the global modifier.
Invalid example:
Class functions are already part of the object instance and should be declared only with func.
Complete Example
Notes
Note
A class only defines the structure.
To use it, create an instance with Inst.