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


Tutorials



OOP


Object Oriented Programming :



Object oriented programming (OOP) involves the development of applications with modular, reusable components.The object-oriented paradigm is built on three important principles:

Encapsulation
Inheritance
Polymorphism

Encapsulation : Encapsulation is the principle of grouping together common functionality and features into a code object.I
nheritance : Inheritance is the principle of transferring the functionality and features of a parent to a child. Since the child is an autonomous unit, the properties and methods inherited by the child can be modified or added to without affecting the parent.
Polymorphism : Polymorphism allows the redefining of methods for derived classes while enforcing a common interface.

Creating an object:
To create an object (i.e., an instance) of a declared class, there are two steps. First, declare a handle to the class (a handle is a reference to the class instance, or object)

class_name handle_name;

Then, call the new() class method:


handle_name = new();


or simple


class_name handle_name = new();


new() is a default implementation which simply allocates memory for the object and returns a handle to the object.

Assignment :


Packet p1;
p1 = new();
Packet p2;
p2 = p1;


In this case, there is still only one object. This single object can be referred to with either variable, p1 or p2.

The following code:


p2 = new p1;


makes a shallow copy of the object referenced by p1, and sets p2 to point to it. A shallow copy creates a new object and copies the values of all properties from the source object. It is a shallow copy because it does not make a copy of any nested objects.


p2 = object_copy(p1); // creates a duplicate object of p, with the handle q .


EXAMPLE : copy

class A{
integer j;
task new(){ j=5;}
}
class B{
integer i;
A a;
task new() {i = 1;}
}
task test(){
B b1 = new(); // Create an object of class B
B b2; //Create a null variable of class B
b1.a = new; //Create an object of class A
b2 = new b1; // Create an object that is a copy of b1,
//but only copies the handle a, not the object
//referenced by a.
b2.i = 10; // i is changed in b2, but not b1
printf("i in b2 = %0d\n", b2.i);// i equals 10
printf("i in b1 = %0d\n", b1.i);// i equals 1
//where as:
b2.a.j = 50; // Change j in the object referenced
// by a. j is shared by both b1 and b2
printf("j is %0d in b1 and %d in b2\n", b1.a.j, b2.a.j);
}
program shallow_copy{
test();
}

RESULTS

i in b2 = 10
i in b1 = 1
j is 50 in b1 and 50 in b2


Properties:



The properties in a class may be of the following atomic types or may
be arrays of these types.
reg
reg [msb:0]
integer
string
event
class type
enum type
A property declaration may be preceded by one of these keywords:
local
public
protected
public is the default protection level for class members. Using public when declaring a property allows global access to that member via class_name.member. In contrast, a member designated as local isone that is only visible from within the class itself. A protected property is not visible outside of the class scope, but it is visible to peer objects and can be inherited by subclasses.



This:



The this keyword is used to unambiguously refer to properties or methods of the current instance. For example, the following declaration is a common, way to write an initialization task:


EXAMPLE : this
class Demo
{
integer x;
task new (integer x)
{
this.x = x;
}
}

program main {
Demo D;
D = new(10);
printf(" D.x is %d \n",D.x);
}
RESULTS

D.x is 10




The x is now both a property of the class and an argument to the task new(). In the task new(), an unqualified reference to x will be resolved by looking at the innermost scope, in this case the subroutine argument declaration. To access the instance property,we qualify it with this to refer to the current instance.



Class Extensions :



Subclasses and Inheritance :
OpenVera's OOP implementation provides the capability of inheriting from a base class and extending its capabilities within a
subclass. This concept is called inheritance. When one inherits from a class into another, the original class definition is not changed, however the new subclass contains all the properties and methods of the base class and then can optionally add additional properties and methods.



EXAMPLE : Inheritance
class A {
task disp_a (){
printf(" This is class A ");
}
}

class EA extends A {
task disp_ea (){
printf(" This is Extended class A ");
}
}

program main {
EA my_ea;
my_ea = new();

my_ea.disp_a();
my_ea.disp_ea();
}
RESULTS

This is class A
This is Extended class A


Polymorphism :



Polymorphism allows the redefining of methods for derived classes while enforcing a common interface.To achieve polymorphism the 'virtual' identifier must be used when defining the base class and method(s) within that class. A virtual class is a class which serves as a template for the construction of derived classes. One cannot create an instance of a virtual class.



EXAMPLE : Polymorphism
class A {
virtual task disp (){
printf(" This is class A ");
}
}

class EA extends A {
task disp (){
printf(" This is Extended class A ");
}
}

program main {
EA my_ea;
A my_a;
my_ea = new();
my_a = my_ea;
my_ea.disp();
my_a.disp();
}

RESULTS

This is Extended class A
This is Extended class A


Super:



The super keyword is used from within a derived class to refer to properties of the parent class. It is necessary to use super when the property of the derived class has been overridden, and cannot be accessed directly.


EXAMPLE :
class A {
virtual task disp (){
printf(" This is class A \n");
}
}

class EA extends A {
task disp (){
super.disp();
printf(" This is Extended class A \n");
}
}

program main {
EA my_ea;
my_ea = new();
my_ea.disp();
}

RESULTS

This is class A
This is Extended class A


Abstract Class:



A set of classes can be created that can be viewed as all being derived from a common base class.If base class is not supposed to be used to creat an object,then it has to be declared as abstract class using keyword virtual.


EXAMPLE:
virtual class BasePacket{
function integer send(bit[31:0] data){
}
}
class EtherPacket extends BasePacket{
function integer send(bit[31:0] data){
// body of the function
...
}
}
Index
Introduction
Data Types
Linked List
Operators Part 1
Operators Part 2
Operators Part 3
Operator Precedence
Control Statements
Procedures And Methods
Interprocess
Fork Join
Shadow Variables
Fork Join Control
Wait Var
Event Sync
Event Trigger
Semaphore
Regions
Mailbox
Timeouts
Oop
Casting
Randomization
Randomization Methods
Constraint Block
Constraint Expression
Variable Ordaring
Aop
Predefined Methods
String Methods
Queue Methods
Dut Communication
Functional Coverage

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