Mailbox using inheritance

Hello!
I’m trying to build a mailbox using inheritance. Here is the code:

class properties;
//declare properties
	int i,j;
	mailbox mbx;
endclass: properties

class data extends properties;
	task op;
		fork		
			begin			
				$display("\n----------------Sending data------------------");			
				for (i=1;i<=5;i++)
				begin: PUT
					#1;		
					mbx.put(i);			
					$display("\ndata sent = %0d at time =%0dns",i,$time);			
				end: PUT
					$display("\n-----------------Receiving data-------");
					begin: GET
						repeat(5)
						begin
							#1;
							mbx.get(j);
				$display("\ndata received = %0d atime = %0dns",j,$time);
						end						
					end:P2
			end
		join
	endtask: op
endclass:data

module mailbox_using_inheritance;

	initial begin
                mailbox mbx = new();
		data h_1 = new();
		h_1.op;
	end
endmodule

The warnings and errors:

# ** Warning: (vlib-34) Library already exists at "work".
# QuestaSim-64 vlog 10.7c Compiler 2018.08 Aug 18 2018
# Start time: 09:03:02 on Apr 12,2022
# vlog -reportprogress 300 mailbox_using_inheritance.sv 
# ** Warning: mailbox_using_inheritance.sv(75): (vlog-2244) Variable 'mbx' is implicitly static. You must either explicitly declare it as static or automatic
# or remove the initialization in the declaration of variable.
# -- Compiling package mailbox_using_inheritance_sv_unit
# -- Compiling module mailbox_using_inheritance
# ** Warning: mailbox_using_inheritance.sv(75): (vlog-2244) Variable 'mbx' is implicitly static. You must either explicitly declare it as static or automatic
# or remove the initialization in the declaration of variable.
# 
# Top level modules:
# 	mailbox_using_inheritance
# End time: 09:03:02 on Apr 12,2022, Elapsed time: 0:00:00
# Errors: 0, Warnings: 2
# vsim work.mailbox_using_inheritance -l run.log 
# Start time: 09:03:02 on Apr 12,2022
# ** Note: (vsim-3812) Design is being optimized...
# ** Error (suppressible): mailbox_using_inheritance.sv(29): (vopt-7063) Failed to find 'mbx' in hierarchical name 'mbx.put'.
#         Region: mailbox_using_inheritance_sv_unit
# ** Error (suppressible): mailbox_using_inheritance.sv(48): (vopt-7063) Failed to find 'mbx' in hierarchical name 'mbx.get'.
#         Region: mailbox_using_inheritance_sv_unit
# Optimization failed
# Error loading design
# Error: Error loading design
#        Pausing macro execution
# MACRO ./run37.do PAUSED at line 3

The o/p I was expectinng:

# --------------Sending data----------------
# 
# data sent = 1 at time = 1ns
# 
# data sent = 2 at time = 2ns
# 
# data sent = 3 at time = 3ns
# 
# data sent = 4 at time = 4ns
# 
# data sent = 5 at time = 5ns
# 
# -----------------Receiving data----------------
# 
# data received = 1 at time = 6ns
# 
# data received = 2 at time = 7ns
# 
# data received = 3 at time = 8ns
# 
# data received = 4 at time = 9ns
# 
# data received = 5 at time = 10ns

Hope I’m clear with my question.

In reply to Shashank Gurijala:

Hi all! I created a memory for my mailbox mbx in the main module and it’s working fine.

But my mailbx is already in the class and i’ve created memory for my class. So my doubt is, why do we need to allocate memory separatey for both mailbox and the class??

This is the change i’ve made in the main module:

module mailbox_using_inheritance;

initial begin

	data h_1 = new();

	h_1.mbx = new(); //even though the class is instantiated, the mail box also needs memory.
	
	h_1.op;

end

endmodule

In reply to Shashank Gurijala:

The code you originally posted does not match the error you reported. The begin:GET label does not match the end:P2 label. The only way you would get those errors is if you did not declare mbx as a mailbox.

Yes, you need to construct a class object before you can reference it; this has nothing to do with inheritance. You declared a mailbox mbx class variable in your module, and another in your class properties, but you left that variable null. You could have written

module mailbox_using_inheritance;

  mailbox mbx = new();
	initial begin
		data h_1 = new();
                h_1.mbx = mbx;
		h_1.op;
	end
endmodule

https://verificationacademy.com/forums/systemverilog/function-arguments-not-initializing-variable-inside-body#reply-54684