-
Notifications
You must be signed in to change notification settings - Fork 0
Home
ChitChat is a toy language designed to explain the basics of object oriented programming. It's goal is to provide an easy and friendly introduction to some important concepts, before switching to a real programming language.
When we write a program, we are basically creating our little world. We can define what is part of our own little world, and we can decide the rules. Because we can decide what is part of our world and what is not, let's start creating a world that has cats and cats only (wouldn't it be awesome? A world with no mosquitoes, no broccoli, only cats... everyone likes cats!)
This is how we would do that:
a cat is a kind of being
This is a declaration. We are just saying that in our world there exist a kind of being called cat. Not a particular cat for now, just the idea of a cat. This "general idea of a cat" is what programmers would call the cat "class".
The mere idea of a cat is not particularly interesting by itself. So far the only thing that we know about cats is that they exist. So let's try to make thing more interesting by defining how a cat behaves when you interact with it.
In ChitChat, you interact with beings by telling them things, so we just have to declare what a cat does when we tell it something:
when a cat is told "get off the sofa!" it replies "meow :)"
So far so good: we just decided that when a cat is told to get off the sofa, it just meows back. When you think about this, we just defined a message, "get off the sofa!", and defined what a cat does when it receives this message. Programmers call this message and the behavior triggered by it a "method" defined on the class cat.
Note that we decided that this is the behavior of any cat, not of a particular one. The next step will be to create a particular cat, so we can finally interact with it.
This is how we create a particular cat called Tandoori (isn't that a great name for a cat?):
Tandoori is a cat
And, of course, we can now interact with Tandoori:
tell Tandoori "get off the sofa!"
Tandoori should reply with a loud meow :), as expected.
Now, a particular cat not only has a behavior, but also some characteristics, like a color or an age.
This is very simple to do in ChitChat:
Tandoori's color is "orange"
Tandoori's age is 3
This makes Tandoori different from other cats. In fact we can create another cat, with different characteristics:
Macao is a cat
Macao's color is "black"
Macao's age is 2
When you send a message to a cat (or, in programmers' speak, call a method) you can also send some information necessary for our cat to respond properly. For example, let's say we want to ask to a cat whether it's older or younger than another cat. Here is how we do that:
when a cat is told "are you older than other?" given other it replies if its age > other's age then "yes" else "no"
And now we can ask Tandoori if it is older than Macao:
tell Tandoori "are you older than other?" given other: Macao
Tandoori will happily reply "yes", because it's 3 years old, while Macao is only 2.
Not all cats are equal. There are different breeds, and some of them behave differently. For example, siamese cats are just like normal cats in many ways, but they are very very vocal.
Let's have siamese cats in our little world too. First of all, a siamese is just a particular kind of cat:
a siamese is a kind of cat
But a siamese cat is more vocal:
when a siamese is told "get off the sofa!" it replies "meow meow meow meow meow meow meow!"
So now, a siamese cat will behave in general just as a normal cat when it comes to asking whether it's older than other cats. After all, a siamese cat is a cat:
Zoe is a siamese
Zoe's color is "cream white"
Zoe's age is 4
tell Zoe "are you older than other?" given other: Tandoori
Zoe will reply yes, as expected from a cat.
But when we tell Zoe to get off the sofa, it will be much louder, because it is
a siamese:
tell Zoe "get off the sofa!"
Zoe, being a siamese, will reply meow meow meow meow meow meow meow!
This way of defining a different "breed" of a class that add some more behavior is usually called "inheritance" by programmers, and we say that the class siamese "inherits from" or "extends" the class cat, providing some specific behavior.