Regarding memory allocation of an object in SV

How a method of a class can be called after deallocated the object of that particular class.

Please find the below code where i mentioned the comments as per my knowledge.

can you please correct me if any comments in the code are wrong.

EXAMPLE: without virtual
class A ;
task disp ();
$display(" This is class A ");
endtask
endclass

class B extends A ;
task disp ();
$display(" This is Extended class A ");
endtask
endclass

program main ;
B B1; //Handle B1 is declared for an object B
A A1; //Handle A1 is declared for an object A

initial
begin
A1 = new(); //1)object will be created 2) construct the object 3) Assign the address of an object A to A1.
A1.disp(); // call the disp() task of parent class

B1 = new(); //1)object will be created 2) construct the object 3) Assign the address of an object B to B1.
A1 = B1; // assigning B1 pointer to A1 i.e Both A1 and B1 Pointing to same object i.e object B
(1) My question in this case is an object A with out handle leads to deallocation of an object A then
if you call A1.disp() task how it will give result as “This is class A”
(2) how we can call a parent class method with out an object A

A1.disp();
end
endprogram

RESULTS

This is class A
This is class A (It should not be displayed because there is no Object A i.e Object A handle is assigned to Object B by using equation A1 = B1, it indicates that Object A is deallocated)

  1. The basic fundamental of OOPS is the child class instance can access the properties and methods of parent class.
    2.How child class instance can access properties and functions of base class without creating base class??
    Ans :: Creating instance of child class (B B1 = new();) meaning calling new function of child class, in the new() function of child class super.new() gets called implicitly meaning memory allocation happened for base class properties, then child class properties gets memory allocation and now new() returns a pointer which points to the contents of both base and child class. This is the reason why child class instance can access parents properties or methods.

  2. A class type can hold a handle corresponding to its type only.

coming to your example
1.initial
2.begin
3.A1 = new();
4.A1.disp();
5.B1 = new();
6.A1 = B1;
7.A1.disp();
8.end
9.endprogram

Line no. 6, Handle of Base class is assigned to A1 meaning as A1 is class type variable of class A, it can only hold pointer to that particular type . As already mentioned handle of B1 points to both properties of A and B classes.

A1.disp() will call parent class method as A1 can only hold handle of class type A it can’t hold pointer to class B properties.

In reply to sureshreddi:

Thanks a lot for your reply.

thanks suresh for the detailed explanation.

pkoti,
If you are expecting output as " This is Extended class A ". this can be achieved by using virtual keyword before the task disp() in base class.

explanation for the line — A1 = B1;
as suresh said, A1 will not hold pointer to base class. But because of this assignment, whatever the virtual functions are there in A1(base), they will be overridden by the fuctions in B1(extended). this overriding is true for variables as well. But using A1 handle, you can not access the variables of B1 which are not defined in A1.

regarding deallocation, I dont think A1 will be deallocated because of this assignment.

In Verilog :-

int a;
int a;

you will get an error such that " re declaration of same variable ‘a’"

In System Verilog;-

class A;

int a;

task disp()
$display(“This is Parent Class”);

endclass

class B extends class A;

int a;

task disp()
$display(“This is child Class”);

endclass

Program main

initial
begin
A A1;
B B1;
B1 = new();

Why i am not get an error such that “re declaration of same variable ‘a’ and method disp() in an Object B”? (here tasks are not virtual. If i mentioned tasks are virtual then variable and tasks can be overrideen)

In verilog we refer a variable through its name in a particular module.so, if we have 2 variables of same name it’s a conflict so that may be the reason they made it a sytax error in verilog.
Coming to sytem verilog we refer a variable in a class through the handle of that object.
Parent class doesn’t have any idea regarding the properties of Child class.So no conflict while accessing variable “a” with A1 handle and when accessing with Child class object it refer to local variable “a” than the parent class variable.
Whatever you declare a variable in extended class is local to that class and not visible to base class so there is no conflict here.
Logically there is no conflict in declaring same variable name in parent as well as child class.

let’s see any expert will present his view.

In reply to sureshreddi:

Thanks For your explanation.

I have one more doubt i.e

if the above code is changed to

In System Verilog;-

class A;

int a=10;
int b =20;

task disp()
$display(“This is Parent Class”);

endclass

class B extends class A;

int c = 30;
int d = 40;

task disp()
$display(“This is child Class”);

endclass

Program main

initial
begin
A A1;
B B1;
B1 = new();
A1=B1;
$display(B1.a);
$display(B1.c);

OUTPUTS:

10
30

Pls correct me if output results are wrong

My doubt is

Is it possible to access the base class properties and methods using child class handle(i.e $display(B1.a)).

If the above statement is correct then pls find the below code.

In System Verilog;-

class A;

int a=10;
int b =20;

task disp()
$display(“This is Parent Class”);

endclass

class B extends class A;

int a = 30;
int b = 40;

task disp()
$display(“This is child Class”);

endclass

Program main

initial
begin
A A1;
B B1;
B1 = new();
A1=B1;
$display(B1.a);

What is the output.

30 or 10

$display(B1.a); ------> how child class handle choose the base class properties of variable ‘a’ and child class properties of variable ‘a’.

In reply to pkoti0583:

The ouput of B1.a = 30, as it refers to its(Class B) local member a.