Layout Options

Which layout option do you want to use?

Color Schemes

Which theme color do you want to use? Select from here.

Programming What is a class?

Joined
Jul 20, 2025
Messages
154
Functions
In math, we know something like $f(x) = x + 1$ and we'd get Input `3' to Output `4' . Then: What is a function?
A function is a rule that makes inputs into outputs.


Functions can represent behavior:

Haskell:
  circleArea :: Double -> Double
  circleArea radius = 3.14 * radius ^ 2

Things get messy
Lets say now we want to do


Haskell:
  circleArea :: Double -> Double
  circleArea radius = 3.14 * radius ^ 2

  circleCircumference :: Double -> Double
  circleCircumference radius = 2 * 3.14 * radius

  circleDiameter :: Double -> Double
  circleDiameter radius = 2 * radius

  radius :: Double
  radius = 1.0

  circleDiameter radius
  circleCircumference radius
  circleArea radius


Notice how they all operate on the same thing: radius.


Functions cluster around data?
We now have our circles radius, and functions that act on that. The radius is data, and those functions add on it. We have: data + functions that act on it. This is what a class brings together


So what is a class?
A class is a bundle of data and functions that operate on the data. So instead of circleArea(radius) we write circle.area(). We attach the function to the data.But, radii of circles does not really demonstrate my point properly. I will switch.


Can we express a class in math?
A class can be a pair?:
$C = (S,M)$
Where: $S$ is the data and $M$ is a set of methods that transform the state. Each method is a function. For example we have a bank account:

\begin{equation}
S = \{ bank balance, account owner \}
F = \{ deposit,withdraw \}
\end{equation}

and each function looks something like

\begin{equation}
deposit: S \times amount \to S
\end{equation}
So, the class transfors the state. So a class is a state space + state transition functions


What do classes do?
Classes let us make many states that have the same rules. Say we have three bank accounts with their own states. They will share the same functions. [therefore] classes separate rules and instances.


So then a class defines state machine?
Yes. methods are transitions. Each method moves the bank account from one state to another, and an object is an instance running that.
 
The last of the witnesses
Joined
Jan 30, 2026
Messages
1,618
OOP notes from class?
...wait…
That actually slaps, teach me you're ways senpai.
This is the kind of stuff that makes me take a peak at your git but I know I can't :'(
 
Activity
So far there's no one here
Top