Friday, March 02, 2012

Super

Super

The super keyword is used from within a derived class to refer to members of the parent class. It is necessary to use super to access members of a parent class when those members are overridden by the derived class.

class Packet; //parent class
integer value;
function integer delay();
delay = value * value;
endfunction
endclass
class LinkedPacket extends Packet; //derived class
integer value;
function integer delay();
delay = super.delay()+ value * super.value;
endfunction
endclass

class base_class;
logic [7:0] H1_pointer;
logic [7:0] H2_pointer;
function new;
H1_pointer = 8'h90;
H2_pointer = 8'h57;
endfunction : new
function display_pointer();
$display("H1 = %0h\t\t H2 = %0h\n", H1_pointer, H2_pointer);
endfunction : display_pointer
endclass : base_class

class derived_class extends base_class;
logic [7:0] H1_pointer;
logic [7:0] H2_pointer;
logic [7:0] H3_pointer;
function display_pointer();
$display ("===============================================================================\n");
$display ("INSIDE THE CLASS CALLED : derived_class -------> CALLING BASE CLASS FUNCTION : display_pointer\n");
$display ("===============================================================================\n");
super.display_pointer();
$display ("===============================================================================\n");
H2_pointer = super.H2_pointer;
$display ("\n\n\n\n==========================================================================\n");
$display ("DERIVED CLASS H1, H2, H3 VALUE\n");
$display ("===============================================================================\n");
$display ("H1 = %0h\t\t H2 = %0h\t\tH3 = %0h\n", H1_pointer, H2_pointer, H3_pointer);
$display ("===============================================================================\n");
endfunction : display_pointer
endclass : derived_class

program super_example;
base_class base;
derived_class derived;
initial begin
base = new;
derived = new;
derived.H1_pointer = 8'h91;
derived.H2_pointer = 8'h58;
derived.H3_pointer = 8'h01;
derived.display_pointer;
end
endprogram : super_example


OUTPUT:
=====================================================================================

INSIDE THE CLASS CALLED : derived_class -------> CALLING BASE CLASS FUNCTION : display_pointer

===================================================================================== =====================================================================================

H1 = 90 H2 = 57

===================================================================================== =====================================================================================

DERIVED CLASS H1, H2, H3 VALUE

===================================================================================== =====================================================================================

H1 = 91 H2 = 57 H3 = 1

=====================================================================================

No comments:

Post a Comment

Popular Posts