NAME

@class - Define a class

SYNOPSIS

@class {CLASS-NAME} [: {PARENT-CLASS-NAME}] {
	{@private | @protected | @public} {PRIMITIVE-VARIABLE-NAME}[={DEFAULT-VALUE}]

	{@private | @protected | @public} @{OBJECT-TYPE} {OBJECT-VARIABLE-NAME}

	{@private | @protected | @public} @{POINTER-TYPE}* {POINTER-NAME}[={DEFAULT-VALUE}]

	[@virtual] {@private | @protected | @public} @method {METHOD-NAME} [{ARGUMENTS}] {
		[COMMANDS]
	}

	@constructor {
		[COMMANDS]
	}

	@destructor {
		[COMMANDS]
	}
}

DESCRIPTION

The @class directive is used to define a class. A class is a blueprint for creating objects. It defines the properties and methods that the objects of that class will have.

The @class directive can be used to define a class with or without a parent class. If a parent class is specified, the new class will inherit all of the properties and methods of the parent class. Bash++ only supports single-inheritance. This means that a class can only inherit from one parent class. The parent class must be defined before the child class in the code.

The class name must be a valid Bash++ identifier. This means that it can only contain letters, numbers, and underscores, cannot start with a number, cannot be a reserved word, and cannot contain two consecutive underscores. The class name is case-sensitive. The class name must be unique within the current scope. If a class with the same name already exists in the current scope, an error will be generated.

After defining a class, you can create objects of that class using the @TYPE ID syntax. For example, if you define a class called MyClass, you can create an object of that class using @MyClass myObject. You can also create a pointer to an object of that class using the @TYPE* ID syntax. For example, @MyClass* myObjectPtr=@new MyClass. The pointer will be initialized to point to a new instance of the class.

The class can contain the following:

A note on constructors and destructors: The constructor and destructor are called automatically when an object is created or destroyed. However, there are also two system methods that bookend the creation/destruction of an object: __new and __delete. These methods are called before the constructor and after the destructor, respectively. They are used to allocate and deallocate memory for the object. These methods cannot be overridden.

EXAMPLE

@class DerivedClass : BaseClass {
	@private dataMember="Default value"
	@private @Object* objectMember=@new Object

	@public @method setDataMember data {
		@this.dataMember="$data"
	}

	@constructor {
		echo "DerivedClass constructor called"
	}
}
@DerivedClass derivedObject
@DerivedClass* derivedPointer=@new DerivedClass

@derivedObject.setDataMember "New value"
@derivedPointer.setDataMember "New value"

NOTES

Classes cannot contain commands directly. All commands must be contained within methods, constructors, or destructors. This is because classes are not executed in a shell context. They are simply blueprints for creating objects. If you place commands directly in a class, you’ll get a compiler error along the lines of “code outside of a code entity.”

SEE ALSO