Monday 14 March 2011

Summary of Chapter 2: Fundamentals of Object Oriented Programming: Part 1

The central idea of OOP is that programs are organized as a collection of interacting objects, each with its own data space and functions. Objects can be made reusable because they encapsulate everything they need to operate, can be built with minimal or no external dependencies, and can be highly parameterized.

Classes and Objects
Class- refers to a class declaration or an uninstantiated object
Object- object refers to an instance of a class.

class register;
   local bit[31:0] contents;
   function void write(bit[31:0] d)
       contents = d;
   endfunction
   function bit[31:0] read();
       return contents;
   endfunction
endclass

This very simple class has one member, contents, and two methods, read() and write(). To use this class, you create objects by instantiating the class and then call the object’s methods, as shown below

module top; register r;
   bit[31:0] d;
   initial begin
       r = new();
       r.write(32’h00ff72a8);
      d = r.read();
   end
endmodule

The local attribute on class member contents tells the compiler to strictly enforce the boundaries of the class. If you try to access contents directly, the compiler issues an error. You can only access contents through the publicly available read and write functions. This kind of access control is important to guarantee no dependencies on the internals of the class and thus enable the class to be reused.