In reply to chashy:
Every class have new constructor function. If a class does not provide an explicit user-defined new method, an implicit new method shall be provided automatically. The new method of a derived class shall first call its base class constructor super.new().
Let take your sub_class example.
class sub_class extends base_class;
bit[3:0] id;
bit[2:0] mode;
function new2(bit [3:0] id, bit[2:0] mode=2);
super.new(6);
this.id=id;
this.mode=mode;
endfunction
function display();
super.display();
$display ("[Child] id=%h,mode=%h",id,mode);
endfunction
endclass
It doesn’t have explicit user-defined new class constructor method. new2 method define by you isn’t constructor. so here implicit new method shall be provided automatically which have code something like this.
function new();
super.new();
endfunction
But your base class need super.new(6) instead of super.new() if you don’t have default value to argument data in base new method. so it was giving error when you didn’t define default value.
function new(bit[15:0] data/*=6*/);
this.data=data;
endfunction
so if you don’t define default value to argument in base new method and don’t have explicit call to super.new with argument value then you will get error.
Let fix your example:
class base_class;
bit[15:0] data;
function new(bit[15:0] data/*=6*/);
this.data=data;
endfunction
function display ();
$display ("[Base] data=%0h", data);
endfunction
endclass
class sub_class extends base_class;
bit[3:0] id;
bit[2:0] mode;
//changed new2 to new, Now class have explicit new method
function new(bit [3:0] id, bit[2:0] mode=2);
super.new(6);
this.id=id;
this.mode=mode;
endfunction
function display();
super.display();
$display ("[Child] id=%h,mode=%h",id,mode);
endfunction
endclass
module tb;
initial
begin
sub_class sc1=new(22,3);
//sc1.new2(22,3);
sc1.display();
end
endmodule