encapsulation

Pragma ericanderton at yahoo.removeme.com
Tue Mar 13 10:58:56 PDT 2007


arun s wrote:
> BCS Wrote:
> 
>> Reply to arun,
>>
>>> class A {
>>> private int c=0;
>>> }
>>> void main() {
>>> A a = new A();
>>> printf("%d",a.c);
>>> }
>>> consider the above code snippet..... y there is no error in printf
>>> statement a.c , since attribute "c" is private ????
>>>
>> Everything inside of a module (a.k.a. file) is visible to everything else 
>> inside of that module.
>>
>>
> how is the concept of data hiding and encapsulation implemented in D ?????

Simple. :) Use separate modules/files:

//test.d
class A{
   private int c = 0;
}

//main.d
import test;

void main(){
   A a = new A();
   printf("%d",a.c);
}

 > dmd main test
 > main.d(5): class test3.A member c is not accessible

You're correct about using 'private' or even 'protected' for encapsulation.  As others have stated, keeping everything 
in one file *implicitly* side-steps those rules.  It's a very useful feature if exploited correctly.

-- 
- EricAnderton at yahoo



More information about the Digitalmars-d mailing list