Is a class virtual because it is marked as such? - EDA Playground
Warning! This exercise has been opened in another tab; autosave has been disabled. Close this tab or refresh to reactivate.

 Languages & Libraries

 Tools & Simulators

 Examples

206


11
 
1
2
module M;
3
  
4
  virtual class virtual_base_class;
5
    virtual function void foo; endfunction
6
  endclass
7
      
8
  virtual_base_class p = new;
9
      
10
endmodule
11
     
xxxxxxxxxx
1
 
1
// Code your design here
2
159 views and 0 likes     
 
I was asked; "Is a class virtual because it is marked as such or because it contains at least one pure virtual method?" Hmmm... Good question. Let's find out.
This code compiles fine: https://www.edaplayground.com/x/32HJ because the class base_class is not virtual. However, this does not: https://www.edaplayground.com/x/3RSk because the class is marked as virtual, even though it contains no pure virtual functions. This will not compile, either, because the class is not marked as virtual, but does contain a pure virtual function: https://www.edaplayground.com/x/4Fiv .
So, a class is virtual because it is marked as such, even if it has no pure virtual methods. But, if a class does have a pure virtual method, it must be marked as virtual.

I was asked; "Is a class virtual because it is marked as such or because it contains at least one pure virtual method?" Hmmm... Good question. Let's find out.

This code compiles fine: https://www.edaplayground.com/x/32HJ because the class base_class is not virtual. However, this does not: https://www.edaplayground.com/x/3RSk because the class is marked as virtual, even though it contains no pure virtual functions. This will not compile, either, because the class is not marked as virtual, but does contain a pure virtual function: https://www.edaplayground.com/x/4Fiv .

So, a class is virtual because it is marked as such, even if it has no pure virtual methods. But, if a class does have a pure virtual method, it must be marked as virtual.

51330:0