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

Abstract Class Example

SystemVerilog Classes & OOP

A virtual (abstract) class with a constructor - it cannot be instantiated directly, only its handle is used here.

Open in IDE
testbench.sv
virtual class BaseClass;
	int data;
	
	function new();
		data = 32'hc0de_c0de;
	endfunction
endclass

module tb;
	BaseClass base;
	initial begin
		base = new();
      $display ("data=0x%0h", base.data);
	end
endmodule




/*
Simulation Log:
---------------

irun: 15.20-s038: (c) Copyright 1995-2017 Cadence Design Systems, Inc.
		base = new();
		   |
ncvlog: *E,CNIABC (testbench.sv,12|5): An abstract (virtual) class cannot be instantiated.
irun: *E,VLGERR: An error occurred during parsing.  Review the log file for errors with the code *E and fix those identified problems to proceed.  Exiting with code (status 1).
*/
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.