Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

When The New Function Is Declared As Static Or Virtual

SystemVerilog Classes & OOP

A note that a class constructor may not be virtual or static; the example just runs a plain new() that sets a string field.

Open in IDE
testbench.sv
class ABC;
  string fruit;
  
  // Note that the constructor is defined as "virtual" which is not allowed
  // in SystemVerilog. Class constructor cannot be "static" either
  function new ();
    fruit = "Apple";
  endfunction
  
endclass

// Instantiate the class object and print its contents
module tb;
  	initial begin
      ABC abc = new();
      $display ("fruit = %s", abc.fruit);
    end
endmodule

/* 
Simulation Log:
---------------
ncsim> run
fruit = Apple
ncsim: *W,RNQUIE: Simulation is complete.
ncsim> exit
*/
design.sv
// Code your design here
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.