# Classes

Lua++ has been designed to support real object-oriented programming by implementing classes. They function similarly to Lua tables but eliminate the need for simulated solutions using metamethods. Classes in Lua++ have similar syntax and behaviors to those in TypeScript and are structured like so:

```
class [tag [template-spec] : [base-list]] {
  member-list
}
```

<table><thead><tr><th width="248">Token</th><th>Description</th></tr></thead><tbody><tr><td>tag</td><td>The type name given to the class.</td></tr><tr><td>template-spec</td><td>Optional template specifications.</td></tr><tr><td>base-list</td><td>Optional list of classes this class will derive members from.</td></tr><tr><td>member-list</td><td>List of members.</td></tr></tbody></table>

To demonstrate just how superior classes are to the legacy tables, consider code fragments that calculate the area of a triangle.

```typescript
class Triangle {
  b: number,
  h: number,
  
  constructor(b: number, h: number)
    self.b, self.h = b, h
  end,
  
  const function area(): number ->
    return (self.b * self.h) / 2.0
}

print(Triangle(3, 4).area())
```

Contrast code above with the following implementation in classic Lua:

```lua
local Triangle = { width = 0, height = 0 }
 
function Triangle:set( fWidth, fHeight )
    self.width = fWidth
    self.height = fHeight
end
 
function Triangle:get()
    return { 
       width = self.width, 
       height = self.height 
    }
end
 
function Triangle:area()
    return self.width * self.height / 2.0
end

Triangle:set(100, 300)
print(Triangle:area())
```

As illustrated, Lua++ classes provide a simple and straightforward interface for getting and setting member variables while Lua’s tables perform a similar function but are awkward and inconvenient.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.luaplusplus.org/language-improvements/classes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
