-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass_data_hiding.sv
executable file
·73 lines (69 loc) · 1.79 KB
/
class_data_hiding.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
program class_data_hiding;
class A;
integer data;
local integer addr;
protected integer cmd;
static integer credits;
function new();
begin
data = 100;
addr = 200;
cmd = 1;
credits = 10;
end
endfunction
task printA();
begin
$write ("value of data %0d in A\n", data);
$write ("value of addr %0d in A\n", addr);
$write ("value of cmd %0d in A\n", cmd);
end
endtask
endclass
class B extends A;
task printB();
begin
$write ("value of data %0d in B\n", data);
// Below line will give compile error
//$write ("value of addr %0d in B\n", addr);
$write ("value of cmd %0d in B\n", cmd);
end
endtask
endclass
class C;
A a;
B b;
function new();
begin
a = new();
b = new();
b.data = 2;
end
endfunction
task printC();
begin
$write ("value of data %0d in C\n", a.data);
$write ("value of data %0d in C\n", b.data);
// Below line will give compile error
//$write ("value of addr %0d in C\n", a.addr);
//$write ("value of cmd %0d in C\n", a.cmd);
//$write ("value of addr %0d in C\n", b.addr);
//$write ("value of cmd %0d in C\n", b.cmd);
end
endtask
endclass
initial begin
C c = new();
c.a.printA();
c.b.printB();
c.printC();
$write("value of credits is %0d\n",c.a.credits);
$write("value of credits is %0d\n",c.b.credits);
c.a.credits ++;
$write("value of credits is %0d\n",c.a.credits);
$write("value of credits is %0d\n",c.b.credits);
c.b.credits ++;
$write("value of credits is %0d\n",c.a.credits);
$write("value of credits is %0d\n",c.b.credits);
end
endprogram