Difference between class and structure - muneeb-mbytes/computerArchitectureCourse GitHub Wiki
1. Classes can be empty but struct cannot be empty.
output:
2. We must use the keyword ‘new’ while creating objects of a class. This is not required when creating objects of Structs.
Struct creation = class with new()
3. Classes can be inherited but structures cannot.
4. The struct size is the sum of all the elements in struct.
struct{
bit[15:0] salary;
byte id;
int abc;
}employee_s;
module struct1;
initial begin
employee_s = '{123,25,23};
$display("size is %d",$bits(employee_s));
end
endmodule
Here if we display the $bits of struct then it will display as 56 because “salary” is 16 bits, id is 8 bits, abc is 32 bits. So the sum is 16+8+32=56.
Output:
for class:
class exam;
int a=10;
bit[3:0]b=4'b0001;
byte c=25;
endclass
exam ex;
module clss;
initial begin
ex=new();
$display("class size is %0d",$bits(ex));
end
endmodule
Output:
5. Structures have contiguous memory allocation and classes have random memory allocation.