How to derive a parent class?

Viewed 90

#1

To understand the behavior of new() and inheritance, I made a simple class and added class child_class:

// Example 1

class parent_class;
  logic [31:0] addr;
  logic [7:0] data;
  bit parity;
  
  function new(logic [31:0] add, logic [7:0] dat);
 
    addr = add;
    data = dat;
  endfunction
endclass

class child_class extends parent_class;
  bit [31:0] data;
  static int count;
  
  function new();
    count++;
    data = count;
  endfunction
endclass


module inheritence;
  initial begin
    
    parent_class p; 
    p = new(32'h1234, 8'hff);
    $display("Value of addr = %0d data = %0d",p.addr,p.data);
    p.addr = 10;
    p.data = 20;
    $display("Value of addr = %0d data = %0d",p.addr,p.data);
  end
endmodule

But, when I executed it, I got the error message:

 class child_class extends parent_class;
                                      |
xmvlog: *E,FAABP1 (testbench.sv,21|38): Task/function call, or property/sequence instance does not specify all required formal arguments.
  function new(logic [31:0] add, logic [7:0] dat);

Q1. Why can't I make the child class? How do I correctly derive the class and use it?

#2

If I remove class child_class in my code above,

   class parent_class;
      logic [31:0] addr;
      logic [7:0] data;
      bit parity;
      
      function new(logic [31:0] add, logic [7:0] dat);
     
        addr = add;
        data = dat;
      endfunction
    endclass
    
    
    module inheritence;
      initial begin
        
        parent_class p; 
        p = new(32'h1234, 8'hff);
        $display("Value of addr = %0d data = %0d",p.addr,p.data);
        p.addr = 10;
        p.data = 20;
        $display("Value of addr = %0d data = %0d",p.addr,p.data);
      end
    endmodule

There is no error message for child class. What's the difference between them?

2 Answers

From IEEE Std 1800-2017, section 8.7 Constructors:

The new method of a derived class shall first call its base class constructor [super.new() as described in 8.15].

From section 8.15 Super:

A super.new call shall be the first statement executed in the constructor. This is because the superclass shall be initialized before the current class and, if the user code does not provide an initialization, the compiler shall insert a call to super.new automatically.

The error message is telling you that the new function in the child_class class is implicitly making a call to the new function in the parent_class class, but that new function requires 2 arguments.

One way to fix the error is to explicitly call spuer.new in child_class with 2 arguments. For example, this code fixes the error:

class child_class extends parent_class;
  bit [31:0] data;
  static int count;
  
  function new();
    super.new(0, data);
    count++;
    data = count;
  endfunction
endclass

You must decide if this how you want your code to behave.

To answer the 2nd question, since you no longer have the derived class (child_class) which has the syntax error, you no longer get an error. Your module properly calls new from parent_class with the correct number of arguments (2).

  1. I am not from system-verilog and from C C++/shell.
    Assume that I created a character variable "testing" and I am not using that variable anywhere inside my program. Due to that, one byte wasted when my program at execution mode.
    Assume that I wrote that in a server program inside an infinite loop.
    In this case, it will affect the performance of server program since it is using a variable which is not being used.
    Hence wastage of memory.
    This may affect other program when that program is waiting for free space at RAM/cache from disk.
    Hence better to remove all warnings when we obtain during compilation. This is applicable to all living beings at each planet/universe handling the space across the planets/universe [if applicable:) ]
  2. You need to learn parent class, constructor, destructor/uploaded functions at parent/child class when performing inheritance. https://www.chipverify.com/systemverilog/systemverilog-inheritance
Related