Professional Documents
Culture Documents
Presentation On CLASS
Presentation On CLASS
VERILOG
Presented by:
Rajesh Panda
Design Trainee
Tessolve Semiconductor
CONTENTS
• Overview of CLASS • Data hiding and encapsulation
• Objects • Constant class properties
• Object methods • Abstract classes and virtual
• Constructors methods
• Static class properties • Polymorphism: dynamic method
• Static methods lookup
• Class scope resolution operator
• This
• Out of block declarations
• Assignment, re-naming and copying
• Parameterized classes
• Inheritance and subclasses
• Typedef class
• Overridden members
• Classes and structures
• Super
• Memory management
• Casting
• Chaining constructors
OVERVIEW OF CLASS
• An object’s methods can be accessed using the same syntax used to access class
properties.
• Class properties are freely and broadly available to the methods of the class, but
each method only accesses the properties associated with its object, i.e., its
instance.
CONSTRUCTORS
• System Verilog does not require the complex memory allocation and deallocation of C++.
• Construction of an object is straightforward and garbage collection is implicit and
automatic.
• Every class has a default (built-in) new method.
• The default constructor first calls its parent class constructor (super.new ()) and then
proceeds to initialize each member of the current object to its default (or uninitialized
value).
• new is now being used in two very different contexts with very different semantics.
Example : Packet p1;
p1=new;
EXAMPLE OF CONSTRUCTOR
OUTPUT
OUTPUT
STATIC CLASS PROPERTIES
• Sometimes only one version of a
variable is required to be shared by all
instances. These class properties are
created using the keyword static.
• Static class properties can be used
without creating an object of that type
STATIC METHODS
• Methods can be declared as static.
• A static method is subject to all the class scoping and access rules, but behaves like a
regular subroutine that can be called outside the class, even with no class instantiation.
• A static method has no access to non-static members (class properties or methods), but it
can directly access static class properties or call static methods of the same class.
• Access to non-static members or to the special this handle within the body of a static
method is illegal and results in a compiler error.
• Static methods cannot be virtual.
• A static method is different from a method with static lifetime.
WHAT IS OUTPUT ?
Scope resolution operator
OUTPUT
THIS
• It is desirable to restrict access to class properties and methods from outside the class by
hiding their names.
• This keeps other programmers from relying on a specific implementation, and it also protects
against accidental modifications to class properties that are internal to the class.
• A member identified as local is available only to methods inside the class. Further, these local
members are not visible within subclasses.
• A protected class property or method has all of the characteristics of a local member, except
that it can be inherited; it is visible to subclasses.
Example for local type
How to access local member by method??
Protected
CONSTANT CLASS PROPERTIES
• Class properties can be made read-only by a 1. Class Jumbo_Packet;
const declaration like any other SystemVerilog 2. const int max_size = 9 * 1024; // global constant
variable. 3. byte payload [];
• Because of class objects are dynamic objects, 4. function new( int size );
5. payload = new[ size > max_size ? max_size : size ];
class properties allow two forms of read-only
6. endfunction
variables: global constants and instance
7. endclass
constants.
• Global constant class properties are those that 8. class Big_Packet;
include an initial value as part of their 9. const int size; // instance constant
declaration. They are similar to other const 10. byte payload [];
variables in that they cannot be assigned a
value anywhere other than in the 11. function new();
declaration. //can be used as static 12. size = $random % 4096; //one assignment in new
• Instance constants do not include an initial 13. payload = new[ size ];
value in their declaration, only the const 14. endfunction
qualifier. This type of constant can be assigned 15. endclass
a value at run-time, but the assignment can only
be done once in the corresponding class
constructor
ABSTRACT CLASSES AND VIRTUAL
METHODS
• A base class sets out the prototype for the subclasses. Since the base class is not intended
to be instantiated, it can be made abstract by specifying the class to be virtual.
• Abstract/virtual classes can also have virtual methods.
• A virtual method overrides a method in all the base classes, whereas a normal method
only overrides a method in that class and its descendants.
• An abstract class can contain methods for which there is only a prototype and no
implementation.
• An abstract/virtual class cannot be instantiated, it can only be derived.
ABSTRACT CLASSES AND VIRTUAL METHODS
(continued………..)
VIRTUAL METHODS (continued………..)
POLYMORPHISM
• Polymorphism allows the use of a variable in the superclass to hold subclass objects, and
to reference the methods of those subclasses directly from the superclass variable.
• Polymorphism means the ability to request that the same Operations be performed by a
wide range of different types of things.
• 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.
CLASS SCOPE RESOLUTION
OPERATOR (::)
• In addition, to disambiguating class scope identifiers, the :: operator also allows access to
static members from outside the class
• Scope resolution operator :: can be used to access to public or protected elements of a
superclass from within the derived classes.
The class scope resolution operator enables the following:
Access to type declarations and enumeration named constants declared inside the
class from outside the class hierarchy or from within derived classes.
CLASS SCOPE RESOLUTION OPERATOR
OUT OF BLOCK DECLARATIONS
• It is convenient to be able to move method definitions out
of the body of the class declaration. Class Packet;
• This is done in two steps. Declare, within the class body, Packet next;
the method prototypes and the full argument specification function Packet get_next();
plus the extern qualifier. get_next = next;
• The extern qualifier indicates that the body of the method endfunction
is to be found outside the declaration. Then, outside the
class declaration, declare the full method—like the extern protected virtual function int send(int value);
prototype but without the qualifiers—and, to tie the endclass
method back to its class, qualify the method name with the
class name and a pair of colons
• The out of block method declaration must match the function int Packet::send(int value);
prototype declaration exactly; the only syntactical ………// body of method
difference is that the method name is preceded by the class ...
name and scope operator (::). endfunction
PARAMETERIZED CLASSES
• It is often useful to define a generic class whose objects can be instantiated to have
different array sizes or data types. This avoids writing similar code for each size or type,
and allows a single specification to be used for objects that are fundamentally different.
• Instances of this class can then be instantiated like modules or interfaces