D Programming - Class access modifiers



Data hiding is one of the important features of Object Oriented Programming which allows preventing the functions of a program to directly access the internal representation of a class type. The access restriction to the class members is specified by the labeled public, private, and protected sections within the class body. The keywords public, private, and protected are called access specifiers.

A class can have multiple public, protected, or private labeled sections. Each section remains in effect until either another section label or the closing right brace of the class body is seen. The default access for members and classes is private.

class Base { 
  
   public: 
  
  // public members go here 
  
   protected: 
  
  // protected members go here 
  
   private: 
  
  // private members go here 
  
};

The Public Members in D

A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member function as shown in the following example −

Example

import std.stdio;

class Line { 
   public:
      double length; 

      double getLength() { 
         return length ; 
      }
      
      void setLength( double len ) { 
         length = len; 
      } 
} 
 
void main( ) { 
   Line line = new Line();
   
   // set line length 
   line.setLength(6.0); 
   writeln("Length of line : ", line.getLength());  
   
   // set line length without member function 
   line.length = 10.0; // OK: because length is public 
   writeln("Length of line : ", line.length); 
} 

When the above code is compiled and executed, it produces the following result −

Length of line : 6 
Length of line : 10 

The Private Members

A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members.

By default all the members of a class are private. For example in the following class width is a private member, which means until you label a member explicitly, it is assumed as a private member −

class Box { 
   double width; 
   public: 
      double length; 
      void setWidth( double wid ); 
      double getWidth( void ); 
}

Practically, you need to define data in private section and related functions in public section so that they can be called from outside of the class as shown in the following program.

import std.stdio;

class Box { 
   public: 
      double length; 

      // Member functions definitions
      double getWidth() { 
         return width ; 
      } 
      void setWidth( double wid ) { 
         width = wid; 
      }

   private: 
      double width; 
}
  
// Main function for the program 
void main( ) { 
   Box box = new Box();
   
   box.length = 10.0; 
   writeln("Length of box : ", box.length);
   
   box.setWidth(10.0);  
   writeln("Width of box : ", box.getWidth()); 
} 

When the above code is compiled and executed, it produces the following result −

Length of box : 10 
Width of box : 10

The Protected Members

A protected member variable or function is very similar to a private member but it provided one additional benefit that they can be accessed in child classes which are called derived classes.

You will learn derived classes and inheritance in next chapter. For now you can check following example where one child class SmallBox is derived from a parent class Box.

The following example is similar to above example and here width member is accessible by any member function of its derived class SmallBox.

import std.stdio;

class Box { 
   protected: 
      double width; 
} 
 
class SmallBox:Box  { // SmallBox is the derived class. 
   public: 
      double getSmallWidth() { 
         return width ; 
      }
	  
      void setSmallWidth( double wid ) {
         width = wid; 
      } 
} 
 
void main( ) { 
   SmallBox box = new SmallBox();  
   
   // set box width using member function 
   box.setSmallWidth(5.0); 
   writeln("Width of box : ", box.getSmallWidth()); 
}

When the above code is compiled and executed, it produces the following result −

Width of box : 5
d_programming_classes_objects.htm
Advertisements