Programming with ooc/Basic

From Wikibooks, open books for an open world
Jump to navigation Jump to search

ooc is an object-oriented programming language. We will start programming in ooc by looking at a simple class: Dog.

Dog: class {
   name: String
   init: func (=name) {}
}

The first line declares a class, with a name : Dog. Classes names start with an uppercase letter.

You will notice here, that every declaration starts with the identifier (name) and is followed by a colon, then the type declaration.

After the class keyword, a block (between braces) follows. It is the body of our class. Then a member variable, the dog's name which is a String (the type of the variable) and a constructor.

Every variable in ooc has a Class. Variables and methods names start with a lowercase letter.

The init method, which is our constructor, seems to do nothing. In fact, it takes a String as parameter and assigns it to the instance variable. We can call our constructor by invoking new:

fido := Dog new("Fido")

The := thing does assign the new instance to a new variable, with type inference. (we do not need to declare the type of fido) Then, we can print fido's name by getting it and invoking println() on it:

fido name println()

And finally, let us store everything in a file named Dog.ooc :

Dog: class {
   name: String
   init: func (=name) {}
}
fido := Dog new("Fido")
fido name println()

Then, invoke the compiler:

rock Dog
./Dog
==> Fido

That's it!