System Verilog Object Oriented Programming and
Classes
IEP on Design Verification and Hardware Security
NIT, Rourkela
OUTLINE
•Classes
•“this” keyword
•Constructors
•Class Assignment
•Shallow Copy and Deep Copy
•Inheritance
•Polymorphism
•Overriding Class members
•“super” keyword
•Casting
•Data hiding and Encapsulation
•Virtual methods and Extern methods
Class Constructors
•The newfunction is called as class
constructor.
•On calling the new method it
allocates the memory and returns the
address to the class handle.
•Constructors should be function and
not a task.
•Constructors does not return values.
•Constructors like any other function
take input parameters.
•There can be only one constructor per
class.
source: -www.verificationguide.com
“this” keyword
•“this” keyword is used to refer to class properties.
•“this” keyword is used to unambiguously refer to class properties or
methods of the current instance.
source: -www.verificationguide.com
Static Class members
•Staticclassmembers:-Classmemberscanbecreatedwiththekeyword
static.classmemberswiththekeywordstaticarecalledasstaticclass
members.Theclasscanhavestaticpropertiesandstaticmethods(functions
andtasks).
•StaticProperties:-Theclasscanhavemultipleinstances,eachinstanceof
theclasswillbehavingitsowncopyofvariables.Sometimesonlyone
versionofavariableisrequiredtobesharedbyallinstances.Theseclass
propertiesarecreatedusingthekeywordstatic.
•A static method can access only static properties of the class and access to
the non-static properties is illegal and lead to a compilation error.
source: -www.verificationguide.com
Class Assignment
•Object will be created only after doing new to an class handle,
•packet pkt_1;
pkt_1 = new();
packet pkt_2;
pkt_2 = pkt_1;
•In the above piece of code, an object is created only for pkt_1, pkt_2 is just
a handle to the packet.
•pkt_1 is assigned to the pkt_2. So only one object has been created, pkt_1
and pkt_2 are two handles both are pointing to the same object
•As both the handles are pointing to the same object any changes made with
respect to pkt_1 willreflecton pkt_2.
System Verilog Inheritance
•Inheritance is an OOP concept that allows the user to create classes that are
built upon existing classes.
•Inheritance is about inheriting base class members to the extended class.
•The new class will be with new properties and methods along with having
access to all the properties and methods of the original class.
source: -www.verificationguide.com
Overriding Class members
•Base class or parent class properties and methods can be overridden in the
child class .
•Defining the class properties and methods with the same name as parent
class in the child class will override the class members.
source: -www.verificationguide.com
“Super” Keyword
•Whenclassmembers are overridden in
the derived class,it is necessary to use
thesuperkeyword to access members
of a parent class.
•With super keyword, it is allowed to
access the class members of parent class
which is only one level up.
source: -www.verificationguide.com
Data hiding and encapsulation
•Data hiding and Encapsulation:
The technique of hiding the data
within the class and making it
available only through the methods
(i.e., by the methods of the class) is
known as encapsulation.
•Access Control:Access control
rules that restrict the members of a
class from being used outside the
class.
•This is achieved by prefixing the
class members with the keywords:
local and keyword.
source: -www.verificationguide.com