Copy Methods

Problem Description

You're given a Transaction class with three fields:

int id;

int addr;

string tag;

Implement function copy_fields() so it returns a new Transaction object with the

same field values as the original.

Your copy() must:

1. Return a new object (not the same handle as this)

2. Set id, addr, and tag on the new object to match the original

After calling copy(), the two objects should hold equal values —

but changing a field on the copy must not change the original.

Example:

Original object t1:

id = 1, addr = 100, tag = "read"

Call: t2 = t1.copy_fields()

t2 now has the same values: id = 1, addr = 100, tag = "read"

Now change t2.id to 99.

Result:

t2.id = 99 (changed, as expected)

t1.id = 1 (still unchanged — t1 and t2 are separate objects)

SystemVerilog

Code Editor Access

Log in to write, run and submit your solution to this interview question. It's free.

Output

Output goes here...