Hi ,
I’m assigning a derived class to a base class as shown in the below example . But , when I’m displaying the value of variable A using the base class handle , I’m seeing that A gets the value in the base class (3) . When classes are assigned to each other , they share a common memory and it isn’t overridden . So , is it right for the value of A to be printed as 3 or should it be 20 ?
class base;
int A =3;
virtual function void cpy( );
A =50 ;
$display("not supposed to be here");
endfunction
endclass
class derived extends base ;
int A =20 ;
function void cpy();
A =100;
$display("derived call here");
endfunction
endclass
module top();
initial begin
derived derived_class2 ;
base base_class2;
derived_class2 =new();
base_class2 = derived_class2 ;
derived_class2.cpy();
base_class2.cpy();
$display("A in derived class2 through cpy =%0d" ,derived_class2.A);
$display("A in base class2 through cpy =%0d" ,base_class2.A);
end
endmodule
OUTPUTS :
derived call here
derived call here
A in derived class2 through cpy =100
A in base class2 through cpy =3