Function new() overriding ? why should we call function in derived class constructor?

class A;
int x;
function new(int a);

class B extends A;
int p;
function new(int a);;

program new_fun ;

A a_o;
B b_o;

a_o = new(1);
b_o = new(100);

i think while creating object for base class( i.e a_o = new(1) ) , we should not call in above code , i suppose to comment function in derived class constructor , then tool showing error message ?

please any one clarify it .

Please have a look at the following link, which explains the above stuff very clearly in a step-by-step manner …

Verification Course Blog | VeriLog Courses | CVC

In reply to RAJESHWAR:

hi rajeshwar ,

t i am clear about that concept please see my question again explain it

In your example, you are trying to construct two objects - an A object with class member x, and a B object with class members x and p.

When you construct the A object with a_o = new(1), its member x will be set to 1.

When you construct the B object with b_0 = new(100), the constructor first calls the constructor and x will be set to 400. When it returns p will be set to 100.

So at the end of your initial block, you will have two objects: an A object with member x set to 1, and a B object with members x set to 400 and member p set to 100.

If you comment out the call to in B’s constructor, SystemVerilog will insert a call to with no arguments for you. Since A constructor requires 1 argument, that implicit call to new() is giving you a compiler error (It is always a good idea to show the error message so we don’t have to guess).

To fix the error, you need to do of three things.

  1. put the call to back.
  2. provide a default value for a in A’s constructor
  3. remove the argument to A’s constructor

In reply to dave_59:

Hi Dave_59 , thank you very much.

one more thing .

in this case ( a_o = new(1) ), the derived class constructor is not calling. so function also won’t call. but why should we keep statement inside derived class constructor ?

even the constructor (new() function ) is not virtual , why the base class object creation is looking for statement.

In reply to kbkdec15:

I think you do not understand how inheritance works.

a_o = new(1) constructs a class A object without knowledge of any classes that may be derived from it. And the fact that you have constructed a class A object has nothing to do with the construction of a separate class B object. So remove the statement a_0 = new(1) from your example and this discussion.

When you extend a base class and then construct the extended class, SystemVerilog (and any other object-oriented language) requires that the new() function of the base class gets executed first, then the new function of the extended class next. By making the first statement of every extended new() function call, this is effectively how the base new function executes first. The base class at the bottom, in your example class A, does not need to call because there is no super class that it was extended from.

In reply to dave_59:

hi dave , thank you very much.

i understood that clearly. we must include while creating extended class object.

in this example. generally the tool should not show error when will delete.
please make sure me about this.

following code should not show any error message because i am creating object for class A, class A is not extends by other class. while creating object the line3 will call then it works good.

but it is not happening like that.

error message are
" super class constructor has non-default arguments. arguments ca be specified by calling explicitly "

" number of actuals and formals does not match in task call "

Line 1: class A;
Line 2: int x;
Line 3: function new(int a);
Line 4: x=a;
Line 5: endfunction
Line 6: endclass

Line 7: class B extends A;
Line 8: int p;
Line 9: function new(int a);

Line10: p=a;
Line11: endfunction
Line12: endclass

Line13: program new_fun ;
Line14: A a_o;
Line15: initial
Line16: begin
Line17: a_o = new(1);
Line18: end
Line19: endprogram

In reply to kbkdec15:

But you have extended class A in your definition of class B. It does not matter that you have not tried to construct class B. You have written an illegal constructor for class B. The class definition is illegal, and the compiler wont except it. It does not matter even if both class A and B are never constructed, the compiler will reject it as it parses it. The following code by itself will not compile

class A;
   function new(int a);
class B extends A;

The compiler will implicitly insert this constructor into class B;

function new;; endfunction

and that call to generates an error because it has no argument required by class A’s new.

In reply to dave_59:

Hi dave thank you . i have got it clearly :) :) :) .

In reply to kbkdec15:

Hi Dave,

Here in one of the post , you have mentioned that “Any OOPs langayage or SV requires that the new() function of the base class gets executed first then the new function of the extended class”. Why it is must to do this for SV or any oops lanaguage.

If new method of derived/extended class doesn’t have any dependency on any properly of base class why it is must ?

In reply to jthakkar:

But someone else could create class C extends B, and class C might have dependencies on A. If class B’s constructor doesn’t call the construction will not work.

Plus there are implementation dependencies in any base class that are not immediately obvious. Class A constructor will allocate memory for its members (x). Class B’s constructor will allocate memory for its members (p) and assume an offset in memory based knowing how much memory class A was going to allocate.

In reply to dave_59:

Hi Jthakkar ,

whenever we extends class , the derived class object can access the base class variable and methods. so memory allocation must be done for base class variables and methods. this can be happen by calling function.

Hi Dave

I have one clarification. If I extend a class, is it necessary to call exclusively in the derived class?

In reply to deepthipatawardhan:
Yes.But if there is no input in the new function of base class, can be omited.It will be excuted by default.Ps:No instruction can be excuted before’s illegal.