Classes extend the notion of ExpL types. A class encapsulates member fields and member functions (called methods in OOP jargon). The following is an example for a class definition. Standard syntax and semantics conventions followed in languages like Java and C++ are assumed.
All the class definitions in a program must be placed together, between the keywords class and endclass. Declaration of class variables follow exactly the same syntax as declaration of user defined type variables in ExpL. The member fields of a class must be declared before member functions are declared. Class definitions must be placed after type definitions, but before global declarations. (Further example programs are provided at the end).
Since OExpL is designed for pedegogical purposes, the following restrictions are imposed to simplify implementation.
a) Methods of a class, apart from its arguments and local variables, have access only to the member fields of the corresponding class.
b) A method can invoke only functions of the same class or functions inherited from its parent class or methods of class variables occuring as member fields of the class. (Be aware of the fragile base class problem).
To put in short
The language supports class extension. A class defined by extension of another class is called a derived class (or child class) of the parent class (sometimes called the base class). The following example demonstrates the syntax of class extension. The language does not support multiple inheritance .
The semantics of class extension can be summarized as follows:
Class variables are declared just like other variables in the global declaration section after type definitions and class definitions.
Object instance is created for a variable of a class with the built-in function new. The language does not support constructors and destructors. Hence intitialization of objects has to be done explicitly. An object can be deallocated using the built-in function delete. The function new will create an object of a specified class at run time, and assigns a reference to the object into a variable. A variable of a given class may be assigned a reference to an object of any desendent class using new. Access semantics of class variables is similar to ExpL user-defined-types, except for the details associated with methods defined within classes. These details are described below.
A variable of a parent class can refer to any object in its inheritance hierarchy. That is, if class B extends class A and class C extends class B, then a variable of class A can refer to objects of classes A, B or C, whereas a variable of class B can refer to any object of class B or C. (References are set using the ExpL assignment statement in the normal way, as with user defined types. The function new can also be used to set the reference for a variable). When a method is invoked with a variable of class B or C, if the method is inherited from an ancestor class, the ancestor's method is invoked. This is illustrated by the following examples.
Suppose that a variable declared to be of a parent class in an inheritance hierarchy holds the reference to an instance of a derived class. On invocation of a method of the parent class variable, if the method is over-ridden by the derived class, the method of the derived class is invoked.
This pivotal feature of object oriented programming compilcates the compiler implementation because at the time of generating code for a method invocation, the correct method to call may not be known, as illustrated in the example below.
In the above code, the value of the variable n read from the input at run-time determines whether the variable arbitrary refers to an object of the class - person or the class - student. Consequently, at compile time, we cannot decide whether the call arbitrary.printDetails() must be translated to an invocation of the function in the parent class or the child class.
To resolve such function invocations, the method of dynamic binding must be implemented using the The virtual function table method .A tutorial explaining the implementation of virtual function tables is given here .
Important Note : If a variable of a parent class holds the reference to an object of a descendent class, only methods defined in the parent class are allowed to be invoked using the variable of the parent class.
Example Programs are here.
The following are the reserved keywords in OExpL and it cannot be used as identifiers.