Vous êtes sur la page 1sur 20

Class Advanced

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

Member disp not found in class A

Abstract Classes

With inheritance we are able to force a subclass to


offer the same properties like their superclasses.
Consequently, objects of a subclass behave like
objects of their superclasses.
Sometimes it make sense to only describe the
properties of a set of objects without knowing the
actual behaviour beforehand.
The need for abstract classes is that you can
generalize the super class from which child
classes can share its methods. The subclass of
an abstract class which can create an object is
called as "concrete class".

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

creating object of virtual class

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.

Scope resolution operator

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:

The class scope resolution operator enables


the following:
Access to static public members (methods
and class properties) from outside the class
hierarchy.
Access to public or protected class
members of a superclass from within the
derived classes.

Multiple Inheritance

Multiple inheritance refers to a feature of some


object-oriented programming languages in
which a class can inherit behaviors and
features from more than one superclass.
Class Advanced

Multiple inheritance allows a class to take on


functionality from multiple other classes, such
as allowing a class named D to inherit from a
class named A, a class named B, and a class
named C.

example

class A;
.....
endclass
class B;
......
endclass
class C;
......
endclass
class D extends A,B,C;
.....
endclass

Class Advanced

Multiple inheritance is not implemented well


in SystemVerilog languages .
Implementation problems include:

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

Class methods and Constraints can be defined in the following


places:
1. inside a class.
2. outside a class in the same file.
3. outside a class in a separate file.
The process of declaring an out of block method involves:
1.declaring the method prototype or constraint within the
class declaration with extern qualifier.
The extern qualifier indicates that the body of the method
(its implementation) or constraint block is to be found
outside the declaration.
NOTE : class scope resolution operator :: should be used
while defining.

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!

Vous aimerez peut-être aussi