|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



OBJECT




Objects are uniquely identifiable by a name. Therefore you could have two distinguishable objects with the same set of values. This is similar to traditional programming languages like verilog where you could have, say two integers i and j both of which equal to 2. Please notice the use of i and j in the last sentence to name the two integers. We refer to the set of values at a particular time as the state of the object.



EXAMPLE:
class simple ;
int i;
int j;

task printf();
$display( i , j );
endtask
endclass

program main;
initial
begin
simple obj_1;
simple obj_2;
obj_1 = new();
obj_2 = new();
obj_1.i = 2;
obj_2.i = 4;
obj_1.printf();
obj_2.printf();
end
endprogram

RESULT

2 0
4 0



Definition (Object) An object is an instance of a class. It can be uniquely identified by its name and it defines a state which is represented by the values of its attributes at a particular time.


The state of the object changes according to the methods which are applied to it. We refer to these possible sequence of state changes as the behaviour of the object.


Definition (Behaviour) The behaviour of an object is defined by the set of methods which can be applied on it.


We now have two main concepts of object-orientation introduced, class and object. Object-oriented programming is therefore the implementation of abstract data types or, in more simple words, the writing of classes. At runtime instances of these classes, the objects, achieve the goal of the program by changing their states. Consequently, you can think of your running program as a collection of objects.



Creating Objects



As you know, a class provides the blueprint for objects; you create an object from a class. In the following statements ,program creates an object and assigns it to a variable:


packet pkt = new(23, 94);
driver drvr = new(pkt,intf);


The first line creates an object of the packet class, and the second create an object of the driver class.

Each of these statements has three parts (discussed in detail below):

1. Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
2. Instantiation: The new keyword is a SV operator that creates the object.
3. Initialization: The new operator is followed by a call to a constructor, which initializes the new object.


Declaration:



Declaring a Variable to Refer to an Object
Previously, you learned that to declare a variable, you write:

type name;

This notifies the compiler that you will use name to refer to data whose type is type. With a primitive variable, this declaration also reserves the proper amount of memory for the variable.

You can also declare a reference variable on its own line. For example:


packet pkt;


If you declare pkt like this, its value will be undetermined until an object is actually created and assigned to it using the new method. Simply declaring a reference variable does not create an object. For that, you need to use the new operator. You must assign an object to pkt before you use it in your code. Otherwise, you will get a compiler error.

A variable in this state, which currently references no object, can be illustrated as follows (the variable name, pkt, plus a reference pointing to nothing): . During simulation, the tool will not allocate memory for this object and error is reported. There will not be any compilation error.



EXAMPLE:
class packet ;
int length = 0;
function new (int l);
length = l;
endfunction
endclass

program main;

initial
begin
packet pkt;
pkt.length = 10;
end

endprogram

RESULT

Error: null object access


Instantiating A Class:



The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.

The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like:


packet pkt = new(10);


Initializing An Object



Here's the code for the packet class:


EXAMPLE
class packet ;
int length = 0;
//constructor
function new (int l);
length = l;
endfunction
endclass


This class contains a single constructor. You can recognize a constructor because its declaration uses the same name as the class and it has no return type. The constructor in the packet class takes one integer arguments, as declared by the code (int l). The following statement provides 10 as value for those arguments:


packet pkt = new(10);


If a class does not explicitly declare any, the SV compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent.



Constructor




SystemVerilog does not require the complex memory allocation and deallocation of C++. Construction of an object is straightforward; and garbage collection, as in Java, is implicit and automatic. There can be no memory leaks or other subtle behavior that is so often the bane of C++ programmers.

The new operation is defined as a function with no return type, and like any other function, it must be nonblocking. Even though new does not specify a return type, the left-hand side of the assignment determines the return type.


Index
Introduction
Class
Object
This
Inheritance
Encapsulation
Polymorphism
Abstract Classes
Parameterised Class
Nested Classes
Constant
Static
Casting
Copy
Scope Resolution Operator
Null
External Declaration
Classes And Structures
Typedef Class
Pure
Other Oops Features
Misc

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer