Question related to inheritance and classes

So, we have three classes (class base_1, base_2, base_3 respectively) and these three classes have their three extended classes(class ext_1,ext_2,ext_3 respectively)
Now the three extended classes have their handles c1,c2 and c3 respectively. Only these three handles are given to you and you’re supposed to tell base class of each extended handle. all the classes are encrypted and nothing can be changed inside them. And no errors should be there while we simulate the code.

In reply to shubh2211:

Where is the question?

Find out the base class of all extended handles given to you.

In reply to shubh2211:

I still don’t understand your question correctly, but here’s my interpretation :

You are given 3 handles, and you are asked to find out what the base classes for these handles are. (This looks like an assignment/interview question which doesn’t have actual code).

If this is UVM you can try calling “get_type_name” on the handles. That will give you the “extended classes”. As you already know the base classes for these “extended classes”, you have the answer.

If it’s not UVM but plain SV, you can use $typename to do the same.

Some sample code below :
With plain SV ::


class A;
endclass

class B extends A;
endclass

module test();
  A a=new();
  B b=new();
  
  initial begin
    $display("typename = %s", $typename(a));
    $display("typename = %s", $typename(b));
  end
endmodule

With UVM ::


import uvm_pkg::*;

class A extends uvm_object;
  `uvm_object_utils(A)

  function new(string name = "default");
    super.new(name);
  endfunction
endclass

class B extends A;
  `uvm_object_utils(B)

  function new(string name = "default");
    super.new(name);
  endfunction
  
endclass

module test();
  A a=new("base_class");
  B b=new("ext_class");
  
  initial begin
    $display("get_name = %s", a.get_name());
    $display("get_type_name = %s", a.get_type_name());
    $display("get_name = %s", b.get_name());
    $display("get_type_name = %s", b.get_type_name());
  end
endmodule

In reply to KillSteal:
Hi killsteal,
sorry but this will not be the solution of this question.
Using this method we’ll get only extended class. But we don’t know the base classes. We just know the names of base classes else everything is encrypted. And we just know that all these extend classes are extended from these base classes only. but which one is extended from which base class is not known. That is what we have to find out.
hope you got the question now.

Hint- related to casting

In reply to shubh2211:

OK, now it’s clear this is a homework/interview puzzle question.

A better way of writing the question would’ve been to say there are three base classes named red, green, yellow. Each class gets extended to three classes apple, banana, orange. You are not able to see the source code to know which of the three extended classes got extended from the three base classes. To solve the puzzle, you need to write a piece of code that compiles and runs without any errors and prints the three associations between base and extended classes.

Hint: a form of casting can be used to solve this puzzle.

In reply to dave_59:
Hi Dave,

Can you please provide the solution to this puzzle?

Thanks and Regards.

In reply to shubh2211:

Use the return value from $cast.

In reply to shubh2211:

Hi, I’ve gone through ur question and what I understood is that

We are given the names of BASE CLASS and EXTENDED CLASS. We need to find out the name of the BASE CLASS of a particular EXTENDED CLASS.


import uvm_pkg::*;
`include "uvm_macros.svh"

class A extends uvm_object;
  `uvm_object_utils(A)
  function new(string name = "default");
    super.new(name);
  endfunction
endclass
 
class B extends uvm_object;
  `uvm_object_utils(B)
  function new(string name = "default");
    super.new(name);
  endfunction
endclass


class AA extends A;
  `uvm_object_utils(AA)
  function new(string name = "default");
    super.new(name);
  endfunction
endclass

class BB extends B;
  `uvm_object_utils(BB)
  function new(string name = "default");
    super.new(name);
  endfunction
endclass
 
module test();
  
  A a=new("base_class");
  B b=new("base_class");
  
  
  AA aa=new("ext_class");
  BB bb=new("ext_class");
 
  initial begin
    
    if($cast(b,aa))
      $display("\nAA is extended from %s", $typename(b));
    else if($cast(a,aa))
      $display("\nAA is extended from %s",$typename(a));
    else 
      $display("Wrong reqirement");
    
    if($cast(b,bb))
      $display("\nBB is extended from %s", $typename(b));
    else if($cast(a,bb))
      $display("\nBB is extended from %s",$typename(a));
    else 
      $display("Wrong requirement");

  end
  
endmodule

Please let me know whether my approach is right or not and if is there any edge case I have missed out on.
Another point I would like to mention is that there is a difference between $typename(class_object_name) and class_object_name.get_type_name(). After executing dynamic casting get_type_name won’t return the original type name but $typename gets us the expected output. Hope this statement is correct!

In reply to Shubhabrata:

Yes, that is the correct approach although a little bit more complicated than it needs to be.

You know that $typename(a) is 'A" because that is the way you declared it.

There is no need to bring UVM into this, you are not even supposed to how these classes are declared. uvm_object’s get_type_name is a virtual method, it always returns the extended type name of the object.

In reply to dave_59:

Ok.Got it. And I didn’t write this code. Just copied from this thread and made few modifications.