// 1. Write a code to add some extra behavior/line_of_code(ex.
Display) in an
existing parent method through child class. (2)
class parent;
int a=1;
virtual function void print();
$display("a=%0d",a);
endfunction
endclass
class child extends parent;
int b=2;
function void print();
super.print();
$display("b=%0d",b);
endfunction
endclass
module tb;
child ch;
initial begin
ch=new();
ch.print();
end
endmodule
// 2. Write a code snippet showcasing the difference in behavior when doing shallow
copy and deep copy.(5)
class sample;
bit [7:0] value;
endclass
class base;
int a;
sample s=new();
function void print(string name);
$display("%0s a=%0d\tvalue=%0d",name,a,s.value);
endfunction
function base copy();
base b = new();
b.s = new();
b.a = this.a;
b.s.value = this.s.value;
return b;
endfunction
endclass
module top;
base b1,b2;
initial begin
b1=new();
b2=new();
b1.a=100;
b1.s.value=20;
$display("Before copying",);
b1.print("B1 Object");
b2.print("B2 Object");
// Shallow Copy
b2=new b1;
$display("After copying by shallow copy",);
b1.print("B1 Object");
b2.print("B2 Object");
//modify b1 content and see b2 is getting modified or not.
b1.a=200;
b1.s.value=30;
b2=new b1;
$display("Modifying B1 Object",);
b1.print("B1 Object");
b2.print("B2 Object");
//modify b2 content and see b1 is getting modified or not.
b2.a=300;
b2.s.value=40;
$display("Modifying B2 Object",);
b2.print("B2 Object");
b1.print("B1 Object");
//deep copy
// b2=b1.copy();
//$display("After copying by deep copy",);
// b1.print("B1 Object");
// b2.print("B2 Object");
//modify b1 content and see b2 is getting modified or not.
// b1.a=200;
// b1.sample.value=30;
// b2=new b1;
// $display("Modifying B1 Object",);
// b1.print("B1 Object");
// b2.print("B2 Object");
//modify b2 content and see b1 is getting modified or not.
// b2.a=300;
// b2.s.value=40;
// $display("Modifying B2 Object",);
// b2.print("B2 Object");
// b1.print("B1 Object");
end
endmodule
// 3. Write a code snippet where a parent class property can be changed from child
class using two different ways (5)
class parent;
int a=1;
function void set(int value);
a=value;
endfunction
endclass
class child extends parent;
function new();
super.a=2;
$display("parent property in child a=%0d",super.a);
endfunction
endclass
//2nd method
class child extends parent;
function void give(int value);
set(20);
endfunction
endclass
module tb;
child ch;
int a;
initial begin
ch=new();
end
endmodule
// 4. Write a class with a property SIZE having default value of 8. Take four
objects of this class such that- First 2 objects have size of 8, third object has
size 10 and fourth object with size 16. (3)
class sample;
int SIZE;
function new(int s=8);
SIZE=s;
endfunction
endclass
module tb;
initial begin
sample s1=new();
sample s2=new();
sample s3=new(10);
sample s4=new(16);
$display("s1 size:%0d",s1.SIZE);
$display("s2 size:%0d",s2.SIZE);
$display("s3 size:%0d",s3.SIZE);
$display("s4 size:%0d",s4.SIZE);
end
endmodule
// 5. Given a base class having some property and an extended class having some
other property, with both these classes having multiple handles. Find a way to find
out which handles are from base class and which are from extended class.
(5)
class base;
int a;
int b;
function new();
$display("handle of base class");
endfunction
endclass
class child extends base;
int c;
int d;
function new();
$display("handle of child class");
endfunction
endclass
module tb;
base b;
child ch;
initial begin
b=new();
ch=new();
end
endmodule