How polymorphism (virtual functons + inheritance )in systemverilog

Hi
I am confused about polymorphism in systemverilog.
Can anyone gives one good example and different scenarios like base class with virual (without)functions and derived classes (with and without) virtual functions.

Regards,
vr

This is something you can easily try yourself, and I think you will understand it better by creating your own example rather than reading someone else’s.

If not, there is always http://bit.ly/xL6elI

Dave

In reply to dave_59:

Hi,

Can any body explain polymorphism in system verilog with some practical examples.
Also the link between virtual methods , abstract class and polymorphism.

Thanks in advance.

In reply to A.D.:

This DVCon paper should answer your quite well: http://go.mentor.com/yin-and-yang

Polymorphism basically allows subclasses to implement an interface from a common base class. To be able to do that, functions and/or tasks in SV are declared as virtual functions/tasks to allow subclasses to override the behavior of the function/task, or add to the behavior if there is a call to super. In general, virtual methods allow a subclass referenced by a base pointer/handle to call the subclass’s implementation of a virtual method, rather than whatever implementation is defined in the base class.

An abstract class is a base class that is not intended to be instantiated, but serves to provide an interface that all subclasses derived from it should implement. In SV, an abstract class is declared virtual, and it can declare prototypes for virtual functions/tasks that are pure virtual methods. Pure virtual methods have no implementation in the abstract base class (ABC), but specifies a requirement for subclasses that they must provide implementations to the pure virtual methods.

For a very simple and contrived example, if Shape is an ABC (makes no sense to instantiate a “shape”), and there are two classes derived from it – Circle and Triangle,

virtual class Shape;  // Abstract base class.
  pure virtual function int unsigned area();  // No implementation for pure virtual method.
endclass : Shape

class Circle extends Shape;
  int unsigned radius;
  virtual function int unsigned area();  // Derivatives of Circle can override this.
    return PI * radius * radius;  // Assume PI is a global constant defined somewhere.
  endfunction : area
endclass : Circle

class Triangle extends Shape;
  int unsigned base, height;
  function int unsigned area();  // Derivatives of Triangle cannot override this.
    return (base * height) / 2;
  endfunction : area
endclass : Triangle

module main;
  Shape s[2];  // Base handles.
  Circle c = new;
  Triangle t = new;
  initial begin
    c.radius = 3;
    t.base = 9;
    t.height = 12;
    s[0] = c;
    s[1] = t;
    $display("The area of the circle is %0d.", s[0].area());
    $display("The area of the triangle is %0d.", s[1].area());
  end
endmodule : main


Since I’ve used integers for simplicity, the answers should be 28 and 54, illustrating how the type-specific method is called through the base handle.

One application is collecting objects of different packet types in an array declared with the base packet type, assuming those packet types are derived from that base. You could iterate through such an array, and call a virtual method that all of these types declare, but implemented differently across the subclasses. In this way, when you handle the collection in the array, you do not care about the specific types of the objects. Instead, you only concern yourself with the common interface that the subclasses implement.

The UVM is a very good example of how polymorphism is used throughout its implementation.

In reply to manning999:

Hi , Thank you for this clear explaination.

In reply to dave_59:

Hi Dave, Thank you for providing this information.

In reply to manning999:

Hello,

I know that once the classes are derived from the Base class (which has a virtual function), all the derived classes’s functions(same name as the function in base class) will automatically be virtual.
Thus, in such case, all the derived classes function (from Triangle class)(int unsigned area()) be also be able to override the area function in Triangle class?

virtual function int unsigned area(); // Derivatives of Circle can override this.

function int unsigned area(); // Derivatives of Triangle cannot override this.

In reply to aadgoyal11:
Actually that was not correct. There is nothing in the current version of SystemVerilog preventing someone from overriding a method. Simple people have suggested that for an enhancement.