Classes: Grouping Data and Behavior
Functions let you reuse code. Classes let you group related data and functions together. Think of a class as a blueprint — define what something looks like and what it can do, then create as many instances as you need.
class Person
string name
integer age
constructor(string personName, integer personAge)
name = personName
age = personAge
functions:
string introduce()
return "Hi, I'm {name} and I'm {age} years old."
start:
Person alice = Person("Alice", 28)
Person bob = Person("Bob", 35)
print(alice.introduce())
print(bob.introduce())
print(alice.name)Hi, I'm Alice and I'm 28 years old.
Hi, I'm Bob and I'm 35 years old.
AliceThe constructor runs when you create a new instance and sets up the initial values. Access fields and methods with dot notation — alice.name, alice.introduce().
Classes can build on each other using inheritance:
class Animal
string name
constructor(string animalName)
name = animalName
functions:
string describe()
return "I am {name}."
class Dog is Animal
string breed
constructor(string dogName, string dogBreed)
base(dogName)
breed = dogBreed
functions:
string describe()
return "I am {name}, a {breed}."
start:
Dog rex = Dog("Rex", "Labrador")
print(rex.describe())
print(rex.name)I am Rex, a Labrador.
RexDog is Animal means Dog gets everything Animal has. base(dogName) calls the parent constructor. Then Dog adds its own breed field and overrides describe().
Quick recap
- A class groups related fields (data) and functions (behavior) together
- constructor(params) runs when you create a new instance
- Access fields and methods with dot notation: alice.name, alice.introduce()
- class Dog is Animal — Dog inherits everything from Animal
- base(args) calls the parent constructor — required when using inheritance
- Override a method by declaring it again in the child class