Chapter 8. Object Oriented Programming

Table of Contents

8.1. Classes and Instances
8.1.1. Instances
8.2. Methods
8.3. Inheritance
8.4. Instance Variables
8.5. Class Variables
8.6. Constructors and Destructors
8.7. Polymorphism
8.7.1. Operator Overloading
8.8. Binary Classes and User Classes

Classes are a powerful feature that helps users to organize a program as a collection of objects. Users that are familiar with C++ will find some syntax similar.

8.1. Classes and Instances

A class is a collection of variables and functions that, together, embody the definition of data type that is distinct and significant for the user's problem. Class functions are called methods. Class variables can be of the two kinds: attributes and class variables. Attributes are more common and do not require any special identifiers. Class variables are defined with the identifier static. We'll discuss class variables later in this chapter. Every class has a name. Here we define a class named Polygon, and give it four attributes:

class Polygon
{
    sides;
    angles;
    dimensions = 2;
    color = nil;
}
	  

Here is another example, Catalog, with one attribute, defined in interactive mode:

Gamma> class Catalog { data;}
(defclass Catalog nil [][data])
	

When you define a class in interactive mode Gamma returns an internal representation of its class definition in Lisp syntax. This definition is a list with the following elements: the defclass function, the class name, the parent class name (nil in this case), the class methods and class variables in one array (none in this example), and the class attributes in a second array.

Default values can be assigned to the attributes. For example, the Polygon class (above) has 2 dimensions and no color by default. The Catalog class has no default data values.

8.1.1. Instances

A class is an abstract data type. A class is used by constructing new instances of it. This is done using the function new:

Gamma> pentagon = new(Polygon);
{Polygon (angles) (color) (dimensions . 2) (sides)}

Gamma> autoparts = new(Catalog);
{Catalog (data)}
	  

In this example, the class is Polygon and the newly-created instance of the class is pentagon. Or, the class is Catalog and the instance is autoparts.

The variables of the instances of a class are called instance variables. They correspond to the attributes in the class definition. In the Polygon class, for example, the instance variables are: sides, angles, dimensions, and color. Note that the function new returns the written representation of an instance, which consists of the class name and a list of instance variables. An instance variable with a default value is represented as a dotted list, such as (dimensions . 2) in our example.

In Gamma, to set or query the instance variable of an instance, the dot notation is used. Thus, each of the instance variables associated with the pentagon instance can now be set as follows:

Gamma> pentagon.angles = 108;
108
Gamma> pentagon.sides = 5;
5
Gamma> pentagon.color = "blue";
"blue"
Gamma> pentagon;
{Polygon (angles . 108) (color . "blue") (dimensions . 2) (sides . 5)}
Gamma>  
	

Notice that internally Gamma holds a class instance and its instance variables together in curly braces. This is called literal instance syntax.