addrpropertydatapropertyformat()method
Preparing questions and your practice workspace.
Preparing questions and your practice workspace.
Chapter 1 · Objects & construction
Separate types, objects, and handles, then initialize inherited and parameterized class state with explicit visibility and ownership.

The language bench / ownership
// Initially: p1 -> P1 { id:7, header:H1 { value:100 } }
p2 = p1;Question directory
Open an authored answer, then use the adjacent object diagram or code example to verify the mechanism.
Start with the SystemVerilog object model, the four OOP ideas, and the visibility rules that keep verification classes reusable without exposing mutable internals.
addrpropertydatapropertyformat()methodaddr = 10addr = 42addr = 91unqualifiedclasschildoutsideprotectedclasschildoutsidelocalclasschildoutsideThe class is one type definition. Each new allocation creates a distinct object, while handle assignment can make multiple names refer to the same object.
A class is a type definition. An object is one allocated instance of that class. A class variable stores a handle that is either null or refers to an object; the handle is not the object itself.
class Packet;
int id;
endclass
Packet p; // null handle
Packet alias_p;
p = new; // allocate one Packet object
p.id = 7;
alias_p = p; // same object, second handle
alias_p.id = 9; // p.id is now 9Encapsulation controls access to state, abstraction exposes the behavior callers need, inheritance extends an existing type, and polymorphism lets one base-typed call select behavior from the actual derived object.
virtual class or pure virtual method can make that contract explicit.| Principle | Question it answers | SystemVerilog mechanism |
|---|---|---|
| Encapsulation | Who may touch this state? | public, protected, local |
| Abstraction | What behavior is promised? | base API, virtual class |
| Inheritance | What definition is extended? | extends and super |
| Polymorphism | Which implementation runs? | virtual method dispatch |
Public members are visible wherever the object is visible. Protected members are visible inside the declaring class and its derived classes. Local members are visible only inside the declaring class.
local for the private-style visibility level; private is not the class-member keyword.protected state when derived verification classes need controlled access, and public methods when outside callers need a stable interface.class Base;
int visible_everywhere; // public by default
protected int visible_to_children;
local int visible_only_here;
endclass
class Child extends Base;
function void configure();
visible_everywhere = 1; // legal
visible_to_children = 2; // legal
// visible_only_here = 3; // illegal
endfunction
endclassNo. A class cannot declare multiple methods with the same name and different argument lists the way C++ or Java can. Use distinct names, default arguments, or a typed wrapper instead.
class MathOps;
function int add_int(int a, int b);
return a + b;
endfunction
function real add_real(real a, real b);
return a + b;
endfunction
endclassA method boundary lets the class preserve invariants, validate updates, and change its representation without forcing every caller to change.
Reason about new(), base-constructor chaining, this and super, parameterized class types, and state shared at class scope.
super.new(args)Base constructorestablish inherited statethis.tag = tagDerived constructorfinish specialized statePacket #(WIDTH, T)WIDTH = 64value parameterT = bit[15:0]type parameterPacket #(64, bit[15:0])A derived constructor establishes inherited state before its own fields. Value and type parameters specialize the class at compile time rather than at object creation.
new allocates an object and runs that class's constructor function to establish valid initial state. The constructor may accept arguments and assign instance properties, but it cannot consume simulation time.
class Packet;
int id;
string kind;
function new(int id = 0, string kind = "READ");
this.id = id;
this.kind = kind;
endfunction
endclass
Packet p = new(7, "WRITE");The base constructor always runs before the derived constructor body. If the base constructor needs arguments, the derived constructor must call super.new(...) as its first executable statement.
class BasePacket;
int id;
function new(int id);
this.id = id;
endfunction
endclass
class TaggedPacket extends BasePacket;
string tag;
function new(int id, string tag);
super.new(id);
this.tag = tag;
endfunction
endclassthis is the handle to the current object. super is a lexical keyword that selects the immediate base-class implementation or constructor; it is not a handle that can be stored.
super.new(...) only in a constructor and with the arguments required by the immediate base.A parameterized class is a family of class types. Value parameters control constants such as width, while type parameters substitute a data type; each distinct parameter tuple is a distinct specialized type.
class Packet #(
parameter int WIDTH = 32,
type T = int
);
rand bit [WIDTH-1:0] data;
T id;
endclass
Packet #() default_packet = new;
Packet #(.WIDTH(64), .T(bit [15:0])) wide_packet = new;A static property has one value shared by every object of that class type. A static method belongs to class scope, cannot use per-object state without an explicit handle, and is normally called with ClassName::method().
class Counter;
static int total = 0;
int serial;
function new();
total++;
serial = total;
endfunction
static function int get_total();
return total;
endfunction
endclass
int count = Counter::get_total();