Object Oriented Programming (OOP)

Contents

ObjAsm32 1.4

Written by Biterider, July 2007 (biterider.AT.gmx.DOT.net)

Introduction

Object-oriented programming, or OOP, is a programming methodology that has become the mainstay of high level languages (HLL) such as Pascal , C++ or SmallTalk. Using ObjAsm32, it is possible now to write elegant assembly programs taking advantage of OOP in the same way as you do using HLLs. The main goal of using an OOP approach in your applications is the encapsulation of data and code that operate on these data into a single program entity. Such an entity is called an object. The data and the related code are collectively known as object members. Some key points about objects are:
  1. The code in an object usually performs some operation on its own data. This code consists of procedures called methods.
  2. The data inside an object is hidden. Only the object's code may directly access the data. You can easily break this rule, as it is usual in assembly language, but you deviate from OOP regulations at your own risk. Object data can be of any type, even other objects.
  3. To use an object, you must first create it in memory. This process is called instantiation and the created object an instance. You can create as many instances as you need in your application, each one with its own data. When the objects are no longer needed, you can destroy them.
  4. You can construct new objects based on existing ones. This technique is called inheritance and makes it possible to reuse a well known and proven code. Using inheritance, you can write an entire application by simply modifying or enhancing some existing object members.
The core of ObjAsm32 is a collection of MASM macros and procedures which implement the backstage work of OOP with minimal overhead.

ObjAsm32 concepts

Like other technologies, OOP comes with its own terms. You will find many of them in the following paragraphs:
Ancestor object
An object that is used to derive another object. The derived object inherits all properties of the ancestor object. Several objects may inherit the properties of a common ancestor object, which provides data and code that are common to all those derived objects. To fulfill their own needs, new specific variables and methods are added to these objects.
Constructor
A special method that initializes an object instance data. In some HLLs like C++, constructors are called automatically. In ObjAsm32, an object can have more than one and it relies on your responsibility to call it. It is also possible that no constructor is necessary to initialize the variables since ObjAsm32 initializes them automatically. During instantiation the initial values are copied from a template that was defined at compile time.
Derived object
An object that inherits the members (variables and code) of an ancestor object. A derived object can be used as an ancestor from which another objects can be derived. The collection of ancestor and derived objects in an object-oriented program creates a hierarchy of related objects. A typical OOP code consists of many object hierarchies. In ObjAsm32, a derived object can inherit the properties of only one ancestor. This is called single inheritance.
Destructor
A special method that is called automatically when an object instance is destroyed. This method is responsible i.e., for freeing allocated resources by the constructor. In ObjAsm32, there exist only one destructor called Done and it must be redefined for each object that allocate specific resources that should be freed together with the object instance. This universal destructor is the minimal common requirement for all objects in this model. The address of this method is placed at the first place in the static method table (SMT).
Encapsulation
The process of relating data and code in an object. A method usually performs some operations on the object's encapsulated data. Encapsulation restricts the use of data to a defined set of procedures, which can simplify debugging, maintenance and revisions.
Inheritance
The process of passing members of an ancestor to a derived object. By using inheritance, you can enhance existing objects quickly and easily modifying existing methods or adding new variables and methods that enrich them.
Instance
Allocated memory used to implement an object. In ObjAsm32, you create object instances using the New macro that allocates memory from the process heap and then initializes it from a template that was generated at compile time. It is possible to change this template so that new instances initialize their variables with different values.
Member
Any component of an object. Members are all methods regardless of their type and variables.
Method
Another term for an object procedure. Basically, a method can be static or dynamic. Static methods can be private to limit their visibility (scope) from other objects or modules. Interface methods are special static methods using to communicate via COM interfaces. A static method address is defined at link-time and it is stored in the static method table (SMT) or the interface method table (IMT), whereas a dynamic method address is stored in the instance data. This allows to redefine it at run-time separately for each object instance. This is called subclassing.
Object
A structure that relates data and code. It's important to understand that an object is merely a source-code description of related data and code. Objects exist solely in the program text; they do not exist at runtime. To use an object in a program, you must create an instance of it. The object concept is equivalent to a class in some HLLs like C++ or Pascal.
Polymorphism
The process by which an object can determine the method to execute. For example, if you have 2 objects, "TriangleShape" and "RectangleShape" derived from a common ancestor called Shape and this ancestor defines a "Draw" method. This method is redefined by the derived objects to render the correct shape. Now it is possible to call the draw method regardless of the object instance. Simply calling the Shape Draw method of any of the derived object instances the correct shape rendering procedure is invoked.
Single inheritance
The technique of building a derived object from a one single ancestor object. Some HLLs support the inheritance of more than one ancestor object. This is called multiple inheritance and is not supported by ObjAsm32.
Static method
An object method whose address is defined at compile time and stored in the static method table. This method address is shared with all instances of a given object. A redefinition of this address will cause an application wide change.
Private method
A static method with limited scope. This restricts the invocation of this methods within methods of the same object.
Interface method
A static method used to implement COM interfaces.
Dynamic method
An object method whose address is stored as an object variable and can be redefined for each instance independently. This is called object subclassing.