# 9.1 Classes and attributes

> A class is a mould: it describes what data every object keeps (its **attributes**) and what it can do (its **methods**).

Web page: https://pseudolearn.app/en/spec/objetos/clase

> **In one sentence**
>
> A class is a mould: it describes what data every object keeps (its **attributes**) and what it can do (its **methods**).

## 9.1.1 How to write it

```pseudolearn
Class «Nombre» ⟦Inherits From «Superclase»⟧
  ⟦Public | Private⟧ Define «atributo» As «tipo»;
  «métodos y constructor»
EndClass
```

> **Parts**
>
> - `Class` opens the declaration and «Name» is the name of the new type.
> - `Inherits From` is optional and names a single [superclass](https://pseudolearn.app/en/spec/objetos/herencia.md).
> - Every attribute is declared with `Define`, without an initial value. It may be preceded by `Public` (the default) or `Private`.
> - Then come the [methods](https://pseudolearn.app/en/spec/objetos/metodos.md) and, at most, one [constructor](https://pseudolearn.app/en/spec/objetos/constructor.md).
> - `EndClass` closes the class.

## 9.1.2 How it works

A class is a sibling unit of the algorithm: it does not go inside it or inside another class. Its name is a new **type**, and every value of that type is an **object** created with [`New`](#objetos.constructor).

### Program: A class with two attributes

```pseudolearn
Class Punto
  Define x As Integer;
  Define yy As Integer;
EndClass

Algorithm Puntos
  Define p As Punto <- New Punto();
  p.x <- 3;
  p.yy <- 4;
  Write p.x, ",", p.yy;
EndAlgorithm
```

**Output**

```text
3,4
```

*The program finishes normally.*

**Figure 9.1-1: A class and its objects**

At the top, the class Punto with the attributes x and yy. Two arrows marked New lead to two objects: one with x = 3 and yy = 4, the other with x = 0 and yy = 7.

A class and two objects created from it. Each object has its own attributes.

Attributes start **without a value**: they accept no initializer. A `Private` attribute can only be used from the code of its own class.

Class-typed variables hold a **reference** to the object, not the object: assigning or passing by value copies the reference and both variables name the same object. Equality `=` between objects compares **identity**. There is no null value you can write.

> **Common mistakes**
>
> ### Program: Reading a private attribute from outside
>
> ```pseudolearn
> Class Caja
>   Private Define secreto As Integer;
> EndClass
>
> Algorithm Espiar
>   Define c As Caja <- New Caja();
>   c.secreto <- 1;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `privateMemberAccess` (error, 7:5): Cannot access private member secreto of class Caja
>
> *The program finishes with an error.*

> **Under the hood**
>
> An object is a heap block with one cell per attribute. The access `p.x` becomes, in the intermediate representation, a field read or write by position.
>
> **Intermediate representation**
>
> ```text
> module Puntos en
> class Punto { x: i64, yy: i64 }
> func suspendible @main() -> unit {
> b0() mass 4:
>   stmt.enter @7:3-7:35
>   %0 = object.new Punto @7:24-7:35
>   stmt.exit @7:3-7:35
>   stmt.enter @8:3-8:11
>   %1 = const.i64 3 @8:10-8:11
>   object.store %0, x, %1 @8:3-8:11
>   stmt.exit @8:3-8:11
>   stmt.enter @9:3-9:12
>   %2 = const.i64 4 @9:11-9:12
>   object.store %0, yy, %2 @9:3-9:12
>   stmt.exit @9:3-9:12
>   stmt.enter @10:3-10:23
>   %3 = object.load %0, x @10:9-10:12
>   %4 = const.char ',' @10:14-10:17
>   %5 = object.load %0, yy @10:19-10:23
>   out.write %3, %4, %5, newline @10:3-10:23
>   stmt.exit @10:3-10:23
>   return @11:1-11:13
> }
> ```

> **Note**
>
> The "cookie cutter and cookie" pair works: one cutter, many cookies, each with its own decoration. It stops working when two variables share an object: they are not two cookies, they are two names for the same one.

**In short**
- A class declares attributes and methods; its name is a type.
- Attributes are declared with `Define` and start without a value.
- `Private` limits access to the class's code.
- Variables hold references; `=` compares identity.

**See also**
- [9.2 Methods and This](https://pseudolearn.app/en/spec/objetos/metodos.md)
- [9.3 Constructors and New](https://pseudolearn.app/en/spec/objetos/constructor.md)
- [9.6 Copies, references and identity](https://pseudolearn.app/en/spec/objetos/copia.md)
