Hi Everyone !!!,
In this Blog we are going to see the concepts about Abstract Class.
A virtual class is a class that cannot be directly constructed.A virtual class is often referred to as an abstract class that cannot be directly constructed.The virtual class is intended to create a template for classes that are extensions of the virtual class.
- An abstract class sets out the prototype for the sub-classes.
- An abstract class cannot be instantiated, it can only be derived.
- An abstract class can contain methods for which there are only a prototype and no implementation, just a method declaration.
virtual class example; //Class defintionendclassVirtual class is not useful until extended.
Example 1:
Abstract classes only be derived not allowed to create an object.
//abstract classvirtual class example1; bit [31:0] data;endclassmodule virtual_class; initial begin example1 p; p = new(); //try to construct an object -> it leads to compilation error endendmoduleOUTPUT:
virtual_class, "p = new();"
Instantiation of the object 'p' can not be done because its type 'example1' is
an abstract base class.
Perhaps there is a derived class that should be used.There is a common misconception that all methods declared in a virtual class are automatically virtual methods. This is not true. For method to be virtual, it must be declared virtual, even in virtual classes.
Abstract method divided into two categories:
- Virtual class methods
- Pure virtual class methods
Virtual
methods are reference models in base class may optionally overridden in derived
class. Virtual methods requires all the arguments and types to be same in the corresponding extended methods. Virtual method may or may not be abstract class.
Rules for extending virtual methods:
Virtual methods numbers, types,name of the argument, return type must match with the same arguments and types of the base class method if overridden.
Rule 1:
Return type should match for overriding in extended class.
//abstract classvirtual class base; virtual function display(int a ,int b); $display("Value of base is %0d %0d", a,b); endfunctionendclass class ex_class extends base; virtual function void display( int a , int b); //Return type should match $display("Value of Extend is %0d %0d", a,b); endfunctionendclass module virtual_class_TB; initial begin base b; ex_class ex; ex=new();
ex.display(4,5); endendmoduleOUTPUT:
Error-[SV-IRT] Incompatible return types
testbench.sv, 10
$unit, "display"
Definition of class function 'base::display' does not have the same return
type as mentioned in the declaration at: "testbench.sv", 4.
1 error
CPU time: .133 seconds to compile
testbench.sv, 10
$unit, "display"
Definition of class function 'base::display' does not have the same return
type as mentioned in the declaration at: "testbench.sv", 4.
1 error
CPU time: .133 seconds to compile
Rule 2:
Number of argument should match.
//abstract classvirtual class base; virtual function void display(int a ,int b); $display("Value of base is %0d %0d", a,b); endfunctionendclass class ex_class extends base; virtual function void display( int a ); //Number of argument should match $display("Value of Extend is %0d", a); endfunctionendclass module virtual_class_TB; initial begin base b; ex_class ex; ex=new();
ex.display(4); endendmoduleOUTPUT:
Error-[SV-INACF] Invalid number of args to class function
Too few arguments in class-method 'display' in derived class 'ex_class'.
Base class-method declared at "testbench.sv", 4
Derived class-method declared at "testbench.sv", 10
Please make sure that correct number of arguments are specified.
1 error
Too few arguments in class-method 'display' in derived class 'ex_class'.
Base class-method declared at "testbench.sv", 4
Derived class-method declared at "testbench.sv", 10
Please make sure that correct number of arguments are specified.
1 error
Rule 3:
Argument type should match for overriding in extended class.
//abstract classvirtual class base; virtual function void display(int a ,int b); $display("Value of base is %0d %0d", a,b); endfunctionendclass class ex_class extends base; virtual function void display( shortint a , int b); //Argument type should match $display("Value of Extend is %0d %0d", a,b); endfunctionendclass module virtual_class_TB; initial begin base b; ex_class ex; ex=new();
ex.display(4,5); endendmoduleOUTPUT:
Error-[SV-ATDNMD] Argument types do not match
testbench.sv, 10
$unit, "a"
The argument type of 'a' for class method 'base::display' in the derived
class does not match the argument type of 'a' for class method in the base
class at: "testbench.sv", 4.
Please check and correct the argument types.
testbench.sv, 10
$unit, "a"
The argument type of 'a' for class method 'base::display' in the derived
class does not match the argument type of 'a' for class method in the base
class at: "testbench.sv", 4.
Please check and correct the argument types.
Rule 4:
Argument direction should match for overriding in extended class.
//abstract classvirtual class base; virtual function display(int a ,int b); $display("Value of base is %0d %0d", a,b); endfunctionendclass class ex_class extends base; virtual function display(output int a , int b); //Argument direction should match $display("Value of Extend is %0d %0d", a,b); endfunctionendclass module virtual_class_TB; initial begin base b; ex_class ex; ex=new();
ex.display(4,5); endendmoduleOUTPUT:
Error-[SV-PDM2] Port direction mismatch
testbench.sv, 10
$unit
The port direction of port 'b' is defined 'output' in the derived class and
'input'in the base class (at testbench.sv, 4).
testbench.sv, 10
$unit
The port direction of port 'b' is defined 'output' in the derived class and
'input'in the base class (at testbench.sv, 4).
Rule 5:
Method name(task/function) should match for overriding in extended class. Refer in above example function names.
Method name(task/function) should match for overriding in extended class. Refer in above example function names.
Rule 6:
If the base class declares a method to be virtual, the extended class method also be virtual.
Pure Virtual Method:
The pure keyword can only be used in an abstract class (virtual class). It is illegal to use pure keyword in a class. The pure virtual method is either pure virtual task or pure virtual function prototype is not allowed to have implementation (no body code allowed) and is not even allowed to have an endtask or endfunction keyword to close the pure virtual method.
The virtual class or method MUST be overridden in the first non-virtual extended class based on the abstract class
Rule 1:
//abstract class
virtual class base; pure virtual function display(int a ,int b); //pure keyword should be used in virtual class otherwise it lead to compilation error & and it should not contain implementation and end function/endtaskendclass class ex_class extends base; virtual function display(int a , int b); $display("Value of Extend is %0d %0d", a,b); endfunctionendclass module virtual_class_TB; initial begin base b; ex_class ex; ex=new();
ex.display(4,5); endendmoduleThe above virtual method Rules are also applicable to pure virtual method.
Difference between Virtual and Pure Virtual?
Virtual methods are reference models in base class may optionally overridden in derived class. Virtual method need not to an abstract class.
Pure Virtual methods are reference models in base class that are MUST be overridden in the derived class. Pure virtual method must be used in abstract class.
Hope this logic's will be useful to everyone !!!
Giving feedback is more precious than writing an article!!! 😊😊😊😊😊
Always welcome both positive and negative feedback's !!!
Feel free to post any queries related SV and UVM
"It is impossible for a man to learn what he thinks he already knows"
-Epicteus
No comments:
Post a Comment