SystemVerilog OOP Mastery · All levels
Pure Virtual Methods as Compile-Time Contracts: Exercises
Exercises for Pure Virtual Methods as Compile-Time Contracts.
Practice exercises
Exercise 1
Design `virtual class monitor_base` with pure virtual `sample()` and `name()` methods, then implement `ahb_monitor`.
Solution
diagram
Declare both methods `pure virtual` in `monitor_base`. In `ahb_monitor extends monitor_base`, provide matching `virtual task sample();` and `virtual function string name();` implementations.Exercise 2
Given a base class with `pure virtual function bit supports_burst();`, add two concrete subclasses for burst and non-burst protocols.
Solution
diagram
Implement `supports_burst()` to return `1` in the burst-capable subclass and `0` in the non-burst subclass. Both subclasses become constructible because the contract is fulfilled.