Class
• A class is a user-defined data type that includes data (class
properties), functions and tasks that operate on data
• functions and tasks are called as methods, both are members of the
class.
• classes allow objects to be dynamically created, deleted, assigned and
accessed via object handles.
Examples
class sv_class;
//class properties
int x;
//method-1
task set(int i);
x = i;
endtask
//method-2
function int get();
return x;
endfunction
endclass
Class Instance and Object Creation
• As we know the class is a data type, Declaring class type variable is
similar to declaring other variables.
sv_class class_1;
• the above statement shows the declaration of variable class_1 with
the type sv_class.
• variable class_1 can contain a handle to an instance of the class
sv_class.
Object Creation
• Class properties and methods can be accessed only after creating the
object.
class_1 = new();
Accessing class properties and methods
• Class properties and methods can be accessed by using
object names followed by property or method name.
class sv_class;
//class properties
int x;
//method-1
task set(int i);
x = i;
endtask
//method-2
function int get();
return x;
endfunction
endclass
module sv_class_ex;
sv_class class_1; //Creating Handle
initial begin
sv_class class_2 = new(); //Creating handle and Object
class_1 = new(); //Creating Object for the Handle
//Accessing Class methods
class_1.set(10);
class_2.set(20);
$display("\tclass_1 :: Value of x = %0d",class_1.get());
$display("\tclass_2 :: Value of x = %0d",class_2.get());
end
endmodule
Example code
class aiit;
int no_of_students; //properties
int y ;
task mult(int a,b);
y = a * b;
endtask
task display();
$display("I am in display task Y = %0x , a = %0x , B = %0x, no_of_students = %0x", y,a,b,no_of_students);
endtask
endclasss
module tb();
aiit aiit_ob1;
aiit aiit_obj2, aiit3;
initial begin
aiit_obj1 = new();
aiit_obj2 = new();
aiit3 = new();
aiit_obj1.no_of_students = 10; //accessing property from outside of the class
aiit_obj1.mult(10,20);
aiit_obj1.display();
aiit_obj2.no_of_students = 10; //accessing property from outside of the class
aiit_obj2.mult(10,20);
aiit_obj2.display();
end
endmodule