|
|
|
|
|
|
|
|
|
|
|
|
Data hiding and encapsulation
|
|
|
By default all the methods and members of the class are accessible outside the class from object pointer. What if we don't want some members and some methods to be not accessable from outside. For this purpose Systemverilog provides local reserve word, when added to a class member to method, becomes local to the object and can not be accessed from outside the object. Even the child class which inherits the parent class can not access the local members to the parent class. |
|
|
|
|
|
In case we want the local members to accessable to a child class, then Systemverilog provides reserve word protected, when added infront of a member, instead of local. A protected class property or method has all of the characteristics of a local member, except that it can be inherited; it is visible to subclasses. |
|
|
|
|
|
|
|
|
|
|
|
Example - Data hiding and encapsulation
|
|
|
|
|
|
1 program class_data_hiding;
2 class A;
3 integer data;
4 local integer addr;
5 protected integer cmd;
6 static integer credits;
7 function new();
8 begin
9 data = 100;
10 addr = 200;
11 cmd = 1;
12 credits = 10;
13 end
14 endfunction
15 task printA();
16 begin
17 $write ("value of data %0d in A\n", data);
18 $write ("value of addr %0d in A\n", addr);
19 $write ("value of cmd %0d in A\n", cmd);
20 end
21 endtask
22 endclass
23
24 class B extends A;
25 task printB();
26 begin
27 $write ("value of data %0d in B\n", data);
28 // Below line will give compile error
29 //$write ("value of addr %0d in B\n", addr);
30 $write ("value of cmd %0d in B\n", cmd);
31 end
32 endtask
33 endclass
34
35 class C;
36 A a;
37 B b;
38 function new();
39 begin
40 a = new();
41 b = new();
42 b.data = 2;
43 end
44 endfunction
45 task printC();
46 begin
47 $write ("value of data %0d in C\n", a.data);
48 $write ("value of data %0d in C\n", b.data);
49 // Below line will give compile error
50 //$write ("value of addr %0d in C\n", a.addr);
51 //$write ("value of cmd %0d in C\n", a.cmd);
52 //$write ("value of addr %0d in C\n", b.addr);
53 //$write ("value of cmd %0d in C\n", b.cmd);
54 end
55 endtask
56 endclass
57
58 initial begin
59 C c = new();
60 c.a.printA();
61 c.b.printB();
62 c.printC();
63 $write("value of credits is %0d\n",c.a.credits);
64 $write("value of credits is %0d\n",c.b.credits);
65 c.a.credits ++;
66 $write("value of credits is %0d\n",c.a.credits);
67 $write("value of credits is %0d\n",c.b.credits);
68 c.b.credits ++;
69 $write("value of credits is %0d\n",c.a.credits);
70 $write("value of credits is %0d\n",c.b.credits);
71 end
72
73 endprogram
You could download file class_data_hiding.sv here
|
|
|
|
|
|
Simulation - Data hiding and encapsulation
|
|
|
|
|
|
value of data 100 in A
value of addr 200 in A
value of cmd 1 in A
value of data 2 in B
value of cmd 1 in B
value of data 100 in C
value of data 2 in C
value of credits is 10
value of credits is 10
value of credits is 11
value of credits is 11
value of credits is 12
value of credits is 12
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 1998-2014 |
Deepak Kumar Tala - All rights reserved |
Do you have any Comment? mail me at:deepak@asic-world.com
|
|