Will abstract class object be created?

Status
Not open for further replies.

jdshah

Junior Member level 3
Joined
Dec 17, 2010
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Ahmedabad
Activity points
1,481
When we call new constructor for derived class, system verilog will call new for base class automatically. Memory for base class will be automatically created. Derived class can not be created with out creating parent class.

"A super.new call shall be the first statement executed in the constructor. This is because the superclass
shall be initialized before the current class and, if the user code does not provide an initialization, the
compiler shall insert a call to super.new automatically."

My question is if base class is abstract class then also it is created by creating derived class? Will its memory be created?
 

Yes it will. Abstract (virtual) classes can contain as many variables, functions and tasks as you like. But they cannot be created directly, they have to be created by using a derived class.

Its quite common to have an abstract base class to define specific behaviour. look at the below example

Code:
virtual class shape;  
  integer n_sides;
endclass : shape

class square extends shape;

  function new;
    super.n_sides = 4;
  endfunction : new

enclass : square;

.......

square x;
x = new;

$display("%d", x.n_sides);
 



Thanks TrickyDicky. It cleared my query.
 

SV 2012 allows a slightly improved instantiation method:

Code:
shape x[3];

x[0] = square::new;
x[1] = triangle::new;
x[2] = hexagon::new;

for(int i = 0 ; i < x.size() ;i++)
  $display("shape = %s , n_sides = %d", x[i].get_name(), x[i].n_sides);
 
Reactions: jdshah

    jdshah

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…