Introduction To Oops

An object-oriented programming is a way programming which enables programmers to think like they are working with real-life entities(a thing with distinct and independent existence) or objects.
In real-life, people have knowledge and can do various work. In OOP, objects have fields to store knowledge/state/data and can do various works — methods.

Basic Terminologies:

Object:

It’s the instance of a class/ it’s the working entity of a class.

Class:

It is a template or blue print about the capability of what an object can do.

Method:

The behaviours of a class. It tells what a method can do.

Instance:

Object and Instance both are same with small difference.

OOPs Concepts:

This session covers the OOP concepts in System Verilog
Basic class syntax, and use of new(), functions and assignments
Inheritance
Polymorphism - use of virtual
Encapsulation - using local, protected, etc
Pure and Virtual Classes
Parameterized classes
Nested classes
Use of static members
Casting from one class variable to another
External functions/tasks and usage of scope resolution
Usage of null and typedef class

class

A class is a user defined data type that includes properties & subroutines.
  • properties means data variable declared in class ,each data part of a class is known as a class property

  • subroutines means tasks and functions

  • classes allow objects to be dynamically created, deleted, assigned, and accessed via object handles.

    synatx :

    class name_of_class;
    -----
    ----- } properties
    -----
    -----
    ----- } method/ functions
    -----
    endclass

    Example:

    class packet;
    int i;
    int j;
    task print();
    $display ("the value of i = %d and j= %d",i,j);
    endtask
    endclass

    Class Instance

    class is a data type, Declaring class type variable is similar to declaring other variables.
    sv_class_packet pkt;
    the above statement shows the declaration of variable pkt with the type sv_class_packet.
    In other words, variable pkt can contain a handle to an instance of the class sv_class_packet.

    Creating New Objects

    An object is an instance of a class
    packet p;
    // Declare a handle that points to an object of the type packet. When a handle is declared it is initialized to null;

    pkt = new();
    //Call the new function to construct the packet object.

    When you call new you are allocating a new block of memory to store variables for that object.
    1. new allocates space for packet
    2. Initializes the variables to their default value (0 for 2state and x for 4-state variables)
    3. Returns the address where the object is store

    Accessing class properties and methods

    Class properties and methods can be accessed by using object names followed by property or method name.
    pkt.src_addr; // call the properties of packet class
    pkt.dst_addr;

    pkt.print(); //calling print method from packet class

    Example:01

    class transaction;
    int i;
    int j;
    task print();
    $display ("the value of i = %d and j= %d",i,j);
    endtask
    endclass


    program main;
    transaction tx;// handle
    initial
    begin
    tx= new(); // create object using new operator and which inilize default value
    tx.print();// call print method from class so which prints default vaules
    end
    endprogram

    Out put

    # ----------------OUTPUT RESULT -----------------------
    # the value of i = 0 and j= 0
    # ------------------------------------------------------

    Example:02

    class packet;

    bit [3:0]src_addr;
    bit [3:0]dst_addr;

    task print();
    $display (" src_addr = %b",src_addr);
    $display (" dst_addr = %b",dst_addr);
    endtask
    endclass


    program main;

    packet pkt;// handle
    initial
    begin
    $display ("--------------- Before Inlize --------------------");
    pkt= new(); // create object using new operator and which inilize default value
    pkt.print();// call print method from class so which prints default vaules
    $display ("--------------- Before After Inlize -------------");
    pkt.src_addr=4'b00_11;
    pkt.dst_addr=4'b11_00; // Inlize properties into som value
    pkt.print();
    end
    endprogram

    Out put

    # ----------------OUTPUT RESULT -----------------------
    # --------------- Before Inlize -------------
    # src_addr = 0000
    # dst_addr = 0000
    # --------------- Before After Inlize ------
    # src_addr = 0011
    # dst_addr = 1100
    # ------------------------------------------------------


    Example:03

    class packet;

    bit [3:0]src_addr;
    bit [3:0]dst_addr;

    task print();
    $display (" src_addr = %b",src_addr);
    $display (" dst_addr = %b",dst_addr);
    endtask
    endclass


    program main;

    packet pkt;// handle
    initial
    begin
    $display ("--------------- Before Inlize --------------------");
    pkt= new(); // create object using new operator and which inilize default value
    pkt.print();// call print method from class so which prints default vaules
    $display ("--------------- Before After Inlize -------------");
    pkt.src_addr=$random();
    pkt.dst_addr=$random(); // which generate random value between range of 0- 15
    pkt.print();
    end
    endprogram

    Out put

    # ----------------OUTPUT RESULT -----------------------
    # --------------- Before Inlize -------------
    # src_addr = 0000
    # dst_addr = 0000
    # --------------- Before After Inlize -------------
    # src_addr = 0100
    # dst_addr = 0001
    # ------------------------------------------------------


    Inheritance

    How do I share code between classes?

     Instantiate a class within another class
     Inherit from one class to another (inheritance/derivation)

    • Inheritance allows you to „add‟ extra:

     Add extra Properties (data members)
     Add extra Methods
     Change the behavior of a method

    • Common code can be grouped into a base class

     Additions and changes can go into the derived class

    • Advantages:

     Reuse existing classes from previous projects with less debug
     Won‟t break what already works

    Inheritance :Example 01

    class parent;
    task print();
    $dispalay (" This is parent calass method ");
    endtask
    endclass

    class child extends parent;
    task print();
    $dispalay (" This is child calass method ");
    endtask endclass
    program main;
    parent p;
    chilld c;

    initial
    begin
    p=new();
    c=new();

    p.print();
    c.print();
    end
    endprogram

    Out put

    # ----------------OUTPUT RESULT -----------------------
    # This is Parent calass method
    # This is child calass method
    # ------------------------------------------------------


    Inheritance :Example 02

    class parent;
    task print();
    $dispalay (" This is parent calass method ");
    endtask
    endclass

    class child extends parent;
    task print();
    $dispalay (" This is child calass method ");
    endtask endclass
    program main;
    chilld c;

    initial
    begin
    c=new();

    p.print();
    c.print();
    end
    endprogram

    Out put

    # ----------------OUTPUT RESULT -----------------------
    # This is child calass method
    # ------------------------------------------------------


    Inheritance :Example 03

    class parent;
    task print();
    $dispalay (" This is parent calass method ");
    endtask
    endclass

    class child extends parent;
    super.print();
    task print();
    $dispalay (" This is child calass method ");
    endtask endclass
    program main;
    chilld c;

    initial
    begin
    c=new();

    c.print();
    end
    endprogram

    Out put

    # ----------------OUTPUT RESULT -----------------------
    # This is parent calass method
    # This is child calass method
    # ------------------------------------------------------


    Inheritance :Example 04

    class parent;
    task print();
    $dispalay (" This is parent calass method ");
    endtask
    endclass

    class child extends parent;
    task print();
    $dispalay (" This is child calass method ");
    endtask
    super.print();
    endclass
    program main;
    chilld c;

    initial
    begin
    c=new();

    c.print();
    end
    endprogram

    Out put

    # ----------------OUTPUT RESULT -----------------------
    # This is child calass method
    # This is parent calass method
    # ------------------------------------------------------


    Coping Objects

    The terms "deep copy" and "shallow copy" refer to the way objects are copied.

    shallow copy :-

    Copying an object with new
    BusTran src, dst;
    initial begin
    src = new ;
    dst = new src ;
    end
    • The above is a shallow copy, similar to photocopyof the original.
    • If the class contains handle to another class, only the top level object is copied by new, not the lower level one.

    Deep Copy :-

    deep copy we have a syntax .copy() which is a user made task in the class. Deep copy copies variable and fields including the objects. Any change in one will not change in the other after a deep copy.