Classes and Objects

A class defines the attributes (member variables), constructors, and behaviors (methods) of objects.

Defining a Class

Syntax

class ClassName{
    attribute1 :: dataType
    attribute2 :: dataType
    
    // Define class
    def ClassName(parameter1, parameter2){
        attribute1 = parameter1
        attribute2 = parameter2
    }
    
    // Define method
    def method1(parameter){        
    }
}
Note:
  • Class names cannot start with a number, contain spaces or any special characters except underscores (_).

  • Parameters cannot have the same name as attributes, otherwise attributes will be overwritten.

  • JIT and destructors are not supported currently.

For example, define a class Student:

class Student {
  name :: STRING
  age :: INT
  score :: DOUBLE VECTOR
  
  def Student(name_, age_, score_) { 
    name = name_
    age = age_
    score = score_
  }
    
  def get_avg_score(){
      return sum(score) / size(score)
  }
  
  def get_info(){
      print("Name:", name)
      print("Age:", age)
      print("Average Score:", get_avg_score())
  }
}Instantiating an Object

Instantiating an Object

A class is a template for objects, and an object is an instance of class.

The following example instantiates an object of class Student:
a = Person("Harrison",20)