SystemVerilog OOP Mastery · All levels
this, Scope Resolution, and Automatic Lifetime: Exercises
Exercises for this, Scope Resolution, and Automatic Lifetime.
Practice exercises
Exercise 1
Create class endpoint with members id and name. Constructor arguments should have same names and use this correctly. Add a display() method.
Solution
diagram
class endpoint;
int id;
string name;
function new(int id, string name);
this.id = id;
this.name = name;
endfunction
function void display();
$display("id=%0d name=%s", this.id, this.name);
endfunction
endclassExercise 2
Write a class hit_counter with static total_hits and per-object local_hits. Add hit() that increments both using class scope resolution for static state.
Solution
diagram
class hit_counter;
static int total_hits = 0;
int local_hits = 0;
function void hit();
this.local_hits++;
hit_counter::total_hits++;
endfunction
endclass