Académique Documents
Professionnel Documents
Culture Documents
Polymorphism
Abstract Class
Scope resolution operator
External declaration
Multiple Inheritance
Polymorphism
Polymorphism allows an entity to take a variety of
representations.
Polymorphism means the ability to request that the same
Operations be performed by a wide range of different
types of things.
Effectively, this means that you can ask many different
objects to perform the same action.
Polymorphism allows the redefining of methods for
derived classes while enforcing a common interface.
To achieve polymorphism the 'virtual' identifier must be
used when defining the base class and method(s) within
that class.
Without virtual
class A ;
task disp ();
$display(" This is class A ");
endtask
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_a = new();
my_a.disp();
my_ea = new();
my_a = my_ea;
my_a.disp();
end
endprogram
RESULTS
This is class A
This is class A
Class Advanced
With Virtual
class A ;
virtual task disp ();
$display(" This is class A ");
endtask
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_a = new();
my_a.disp();
my_ea = new();
my_a = my_ea;
my_a.disp();
end
endprogram
RESULTS
This is class A
This is Extended class A
Class Advanced
Note:
The methods which are added in the
subclasses which are not in the parent class
canot be accessed using the parent class
handle.
This will result in a compilation error. The
compiler check whether the method is
existing the parent class definition or not.
example
class A ;
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass
program main ;
EA my_ea;
A my_a;
Class Advanced
initial
begin
my_ea = new();
my_a = my_ea;
my_ea.disp();
my_a.disp();
end
endprogram
Abstract Classes
example
virtual class A ;
virtual task disp ();
$display(" This is class A ");
endtask
endclass
class EA extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass
program main ;
EA my_ea;
A my_a;
initial
begin
my_ea = new();
my_a = my_ea;
my_ea.disp();
my_a.disp();
end
endprogram
RESULT
This is Extended class A
This is Extended class A
Class Advanced
virtual class A ;
virtual task disp ();
$display(" This is class A ");
endtask
endclass
program main ;
A my_a;
initial
begin
my_a = new();
my_a.disp();
end
endprogram
Class Advanced
RESULT
Abstract class A cannot be instantiated
Virtual Keyword
Virtual keyword is used to express the fact
that derived classes must redefine the
properties to fulfill the desired functionality.
Thus from the abstract class point of view,
the properties are only specified but not
fully defined.(base class)
The full definition including the semantics
of the properties must be provided by
derived classes.
class Base;
typedef enum {bin,oct,dec,hex} radix;
task print( radix r, integer n );
$display(" Enum is %s ",r.name());
$display(" Val is %d",n);
endtask
endclass
program main;
initial
begin
Base b = new;
int bin = 123;
b.print( Base::bin, bin ); // Base::bin and bin are different
end
endprogram
RESULT:
Enum is bin
Val is 123
Note:
Multiple Inheritance
example
class A;
.....
endclass
class B;
......
endclass
class C;
......
endclass
class D extends A,B,C;
.....
endclass
Class Advanced
1. Increased complexity
2. Not being able to explicitly inherit from
3. multiple times from a single class
Order of inheritance changing class
semantics.
Method Overloading
Method overloading is the practice of declaring the same method with different
signatures. The same method name will be used with different data type . This is
Not Supported by SystemVerilog as of 2008.
EXAMPLE:
task my_task(integer i) { ... }
task my_task(string s) { ... }
program test {
integer number = 10;
string name = "vera";
my_task(number);
my_task(name);
}
External declaration
example
class B;
extern task printf();
endclass
B::
task
printf();
$display(" Hi ");
endtask
program main;
initial
begin
B b;
b = new();
b.printf();
end
endprogram
RESULT:
Hi
Class Advanced
Thank You!