0% found this document useful (0 votes)
16 views

4.Sv Object Oriented Programming

Uploaded by

dangthach2002vg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

4.Sv Object Oriented Programming

Uploaded by

dangthach2002vg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

OBJECT ORIENTED PROGRAMMING

OVERVIEW
◼ Object oriented design is a common programming
paradigm
 Data and the means to manipulate is described together in a formal
structure called a class

◼ An instance of a class is referred to as an object. Objects


are dynamically created and destroyed
 Memory allocation and de-allocation is handled automatically

◼ Code minimization and reuse is facilitated through


inheritance and polymorphism
CLASS
introduction
◼ A class is a data type
 Contains data referred to as class properties
 Contains subroutines (task/function) referred to as class methods
 Both properties and methods are members of class
CLASS
object creation

◼ An object is an instance of a class. To create a class


object:
1. Declare a handle (class variable)
◼When a handle is declared it is initialized to null

2. Create the object by invoking the new() method


◼When you call new() you are allocating a new block of memory to
store variable for that object. Initialized the variables to their
default value (0 for 2 state, x for 4 state)
CLASS
object destruction

◼ Objects destruction (de-allocation) done automatically when an


object is no longer being referenced

◼ Need to manually clear all handle by setting them to null


CLASS
accessing object member

◼ Refer to members using the . notation


CLASS
constructor
◼ Every class has a build-in method new() called the
constructor
No return type in declaration
May have arguments, which allows for run-time customization
Used for initialization of the instance
CLASS
static properties

◼ Unless declared static, each class object has its own copy of the
class properties
◼ Static properties shared between all instances of class
◼ Static properties can be used with out creating object
CLASS
const properties
◼ Global constant have an initial value as a part of their declaration. They
can not be assigned a value anywhere other than in the declaration

◼ Instance constant are assigned value in their constructors


CLASS
this

◼ The this keyword is used to unambiguously refer to (non-


static) members of the current instance
CLASS
data hiding: introduction

◼ By default, all members are visible in the scope in which


the object is visible or we can say it be accessed outside
the class directly using the dot operator

◼ This is not always desirable. To prevent accidental and


incorrect manipulation of class properties and invocation
of methods, they can be declared as local or protected

◼ Hiding the properties from being accessed outside the


class is called encapsulate
CLASS
data hiding: local member
◼ Properties and methods can be protected via “local”
CLASS
data hiding: local member
▪ Local members are only available to methods inside the class
CLASS
data hiding: local member
◼ Local members not visible in the derived class
CLASS
data hiding: protected member
◼ A protected member has all of the characteristics of a local
member, except that it can be inherited; it is visible to derived class
RANDOMIZATION
overview

◼ In SV, randomization is achieved via class


◼randomize() function built into class

◼ Two types of random properties are supported


rand: same value may come before all the possible value
have been returned
randc: same value does not get returned until all possible
value have been returned

◼ When the class function randomize() is called


◼Randomizes each rand and randc property value to full range
of its data type
RANDOMIZATION
controlling random variables

◼ The range of value of random variables are restricted


using constraint expression that are declared using
constraint block

◼ Constraint block are class members, like task, function


or variables. Multi constraint blocks may be defined

◼ Restrictions on constraint expressions:


Constraints can be any SV expression with variables of
integral type (bit, byte, int, …)
Constraints support only 2 state value
RANDOMIZATION
constraint: overview
RANDOMIZATION
constraint: relational operator

◼ There can be a maximum of only one relational


operation (<, <=, ==, >, >=) in an expression
RANDOMIZATION
constraint: set membership
◼ Select from a list or set with keyword inside

◼ Excluded from a specified set with !


RANDOMIZATION
constraint: implication
◼ Implication operator: ->
▪ if (…) … [else ..]
RANDOMIZATION
constraint: equivalence
◼ Equivalence operator: <->
 True bidirectional constraint
 A <-> B means if A is true B must be true and if B is true A must be
true
RANDOMIZATION
inline constraint
◼ Syntax:
obj.randomize() with { <new constraints> }
RANDOMIZATION
controlling randomization at runtime
◼ Turn randomization for properties on or off with
task/function int object_name.properity.rand_mode(0|1);
◼ 1 – enable randomization (default)
◼ 0 – disable randomization
RANDOMIZATION
controlling constraint at runtime
◼ Turns constraint block on or off with
task/function int object_name.constraint_block_name.constraint_mode(0|1);
◼ 1 – enable constraint (default)
◼ 0 – disable constraint
OOP INHERITANCE
INHERITANCE
overview
◼ Object-oriented programming
 New classes derived from original (base) class. This class is called a
derived class
 Inherits all contains of base class
INHERITANCE
overriding
◼ Derived classes may “override” the definition of a member
inherited from the base class
 “hides” the overridden property or method
 To override a method the child method’s signature must match the
parent’s exactly
INHERITANCE
super
◼ The super keyword is used to refer to members of the base class
 Required to access members of the parent class when the members
are overridden in the child class
 Only legal from within the derived class
INHERITANCE
$cast
◼ Derived classes compatible with base class

◼ $cast() can also be used as a function that check if the type of the arguments
match. It returns a true if they match and a false if they do not
OOP POLYMORPHISM
POLYMORPHISM
introduction(1)
◼ Consider a base class that is multiply derived into subclasses, each
of which overrides a common base method

◼ Polymorphism allows the use of variable in the base class to hold


subclasses object and to reference the methods of those subclasses
directly from the base class variable

◼ To achieve polymorphism the virtual identifier must be used when


defining the methods within that class
POLYMORPHISM
example(1)
POLYMORPHISM
example(2)
POLYMORPHISM
introduction(2)

You might also like