Chapter 2. Classes and Objects

Table of Contents
2.1. Preliminaries
2.2. Defining Classes and Creating Objects
2.3. Class Data: shared and const
2.4. Routine definitions
2.5. Conditional Execution
2.6. Attribute Accessor Routines
2.7. Static Type Inference
2.8. Class Parameters
2.9. A Running Example: Employees

All entities in Sather are objects, and objects are defined by classes. Even the basic entities in Sather, such as integers and floating point values are objects in Sather. Sather has several different kinds of classes - reference classes, abstract classes, immutable classes, partial classes and external classes. The important kinds of classes are reference classes and abstract classes - the rest are used in restricted circumstances. There are also some special objects (closures) which are not directly defined by classes, but we will defer their discussion till later.

Each Sather object has an associated type which indicates the class that was used to create the object. A variable in Sather also has a type, which indicates the kinds of objects it can be assigned to.

This chapter will focus on the most common kind of classes, reference classes, and the standard constructs used to create classes. Though iterators are an essential component of Sather code, their discussion has been deferred to the next chapter, since they are a relatively novel language feature.

2.1. Preliminaries

To make it easier to present examples in the following sections, we will start by introducting a few basic classes - integers, floating point numbers and strings. We will also describe how to print out data and to use the compiler

2.1.1. Some basic classes - INT, FLT and STR

Though basic numbers and strings enjoy some special language support (such as a means to initialize them to values like 5 or "foo"), they are defined as regular classes, and are a part of the standard library.The FLT class represents floating point numbers, while the INT class represents integers and the STR class represents strings. Variables may be declared to be of any of these classes and assigned when they are declared
a:FLT := 3.0;
b:INT := 5;
c:STR := "foo";

It is also possible to perform the usual operations on these classes, such as addition of numbers and concatenation of strings (represented by the "+" operator):
a:STR := "foo";
b:STR := "bar";         -- + concatentates strings
c:STR := a + b;         -- c is "foobar"
e:INT := 5;
f:INT := 7;
g:INT := e+f;           -- g is 12
compare:BOOL := e > f;  -- compare is false
#OUT + compare;         -- Prints out 'false'

Comments in Sather start with a -- and extend to the end of the line. Note that all variables have a default initial void value. For the present, void may be thought of as either the NULL pointer for reference objects, 0 for integers, 0.0 for floats and false for booleans.

2.1.2. Printing output

You can print data of various types in Sather using the command #OUT+
a:INT := 10;
#OUT + "hello world " + a;  -- Prints out "hello world 10"

Treat '#OUT+' as an idiom for now; it is equivalent to the standard output routines in other languages.

2.1.3. Sather source files

Sather source files consist of lists of classes. In addition to the source files that a user specifies on the command line to the compiler, the standard library files are always implicitly examined. Definitions of the basic classes such as integers and strings as well as containers of all kinds are to be found in the standard library.

Execution of a Sather program begins with a routine named 'main' in a specified class, (a class called 'MAIN' is used by default). If main is declared to have a return value of type INT, this will specify the exit code of the program when it finishes execution.

2.1.4. Hello World

The hello world program is show below:
class HELLO_WORLD is
   main is
      #OUT+"Hello World\n";

As we mentioned earlier, printing to standard output is obtained by calling #OUT+.

If the above code is stored in the file, it can be compiled (using the ICSI Sather compiler) by:
sacomp -main HELLO_WORLD -o hw

The '-main' option simply indicates to the compiler that the main routine will be found in class HELLO_WORLD. The resulting executable, 'hw' can be run as follows
prompt> hw
Hello World