Classes

module class_new;

class abc;

int a;
function new(int in);
a = in;

endfunction

endclass

class abc1 extends abc;
int j;

endclass

abc c1;
abc1 c2;
initial begin 
c1 = new(10);
c2 = new;
$display(c1.a);

end

endmodule

ther following error is seen
** Error: /home/sv/20_10/class_new.sv(13): Super class constructor has non-default arguments. Arguments can be specified in the “extends” clause or by calling super.new() explicitly.
** Error: /home/sv/20_10/class_new.sv(13): Number of actuals and formals does not match in task call.

Why is thsi coming. cant we extend a class which has a new function with an argument?

In reply to rakesh reddy:

Your problem arises from not having a default value for ‘in’ in the new() function of your base class. When a default value for an argument isn’t provided, it becomes a mandatory argument.

The extended class will call super.new() for the base class during as part of the implicit new() function. Since the implicit call to super.new() has no argument, you get this error.

You can fix it by providing a default value for ‘in’ in the base class, or defining the new() function in the extended class and calling super.new() with an argument.

In reply to cgales:

Hi Rakesh Reddy,

module class_new;
 
class abc;
 
int a;
function new(int in);
a = in;
endfunction
 
endclass
class abc1 extends abc;
int j;
  //int a;
  function new(int a);
    super.new(a);
  endfunction
endclass
 
abc c1;
abc1 c2;
initial begin 
c1 = new(10);
  c2 = new(9);
$display(c1.a);
 
end
 
endmodule

Here parent new is expecting argument so in child new method we have to pass argument to super.new(); Try this it will work.
Thanks