Wednesday, 5 November 2014

Access specifiers / Access Modifiers/ Access controls of classes in C++

Access specifiers / Access Modifiers/ Access controls of classes in C++:

In C++ we have three access specifiers available.

Public
Private
protected
Based on the access modifiers the members and member functions accessed by outside the class.
 The access modifiers are followed by colon. We can use three modifiers in the same class to set the boundaries on the members and members function of the class.


Private:
NOTE: The default access modifier of the class is “private” in C++
If the members are declared under private modifier it can be accessed with in the class only. We cannot access the private members outside the class.
The below example shows the usage of the private modifier
using namespace std;
using namespace System;
class TestCopy
{
   public:
       int objectCount;
      int iCnt;
      TestCopy () { cout << "I am in the constructor of the class" << endl; }
      // Constructor definition
      TestCopy(double l=2.0, double b=2.0, double h=2.0) : iCnt (0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // Increase every time object is created
         objectCount++;iCnt++;
      }
      TestCopy (TestCopy &objTestCopy) {
           cout <<"Copy constructor called." << endl;
           length = objTestCopy.length;
           breadth = objTestCopy.breadth;
           height = objTestCopy.height; 
     }
      ~TestCopy ()
      {
        cout << "I am in the destructor of the class" << endl;
      }
      double Volume ()
      {
         return length * breadth * height;
      }
      int GetCnt () {
        return iCnt;
      }
   private:
      double length;     // Length of a TestCopy
      double breadth;    // Breadth of a TestCopy
      double height;     // Height of a TestCopy
};
 int _tmain (int argc, _TCHAR* argv[])
{
   TestCopy ocTestCopy1 (10, 20);
  /* trying to accessing the private data members */
  ocTestCopy1.length; 
  ocTestCopy1.breadth;
  ocTestCopy1.height;
  cout << "TestCopy1's Voilume: " << ocTestCopy1.Volume () << '\n';
 getchar ();
 return 0;
} 

In the above code we are trying to access the members of the class which are declared as private. But the compiler prompts the message that
 Error 2 error C2248: 'TestCopy::length' : cannot access private member declared in class 'TestCopy'  

Similarly, we cannot access the member functions that are declares as private.

Public:
If the members are member function are declared under the public keyword those can be accessed from anywhere.

using namespace std;
using namespace System;
class TestCopy
{
   public:
       int objectCount;
      int iCnt;
      TestCopy () { cout << "I am in the constructor of the class" << endl; }
      // Constructor definition
      TestCopy(double l=2.0, double b=2.0, double h=2.0) : iCnt (0)
      {
         cout <<"Constructor called." << endl;
         length = l;
         breadth = b;
         height = h;
         // Increase every time object is created
         objectCount++;iCnt++;
      }
      TestCopy (TestCopy &objTestCopy) {
           cout <<"Copy constructor called." << endl;
           length = objTestCopy.length;
           breadth = objTestCopy.breadth;
           height = objTestCopy.height; 
     }
      ~TestCopy ()
      {
        cout << "I am in the destructor of the class" << endl;
      }
      double Volume ()
      {
         return length * breadth * height;
      }
      int GetCnt () {
        return iCnt;
      }
public:
      double length;     // Length of a TestCopy
      double breadth;    // Breadth of a TestCopy
      double height;     // Height of a TestCopy
};
int _tmain (int argc, _TCHAR* argv[])
{
  TestCopy ocTestCopy1 (10, 20);
  ocTestCopy1.length;
  ocTestCopy1.breadth;
  ocTestCopy1.height;
  cout << "TestCopy1's Voilume: " << ocTestCopy1.Volume () << '\n';
   getchar ();
 return 0;
}

The above code works fine, because we are trying to access the public members of the class. And these
The member length, breadth & height are accessible from outside of a class since their access modifier is public.

Protected:
The protected members and functions of the class are similar to the private. These have the special feature that the derived classes can access the protected members of the base class.
#include <iostream>
using namespace std;
 class Shape
{
   protected:
      double width;
};
 class SmallShape:Shape // SmallShape is the derived class.
{
   public:
      void setSmallWidth( double wid );
      double getSmallWidth( void );
};
 // Member functions of child class
double SmallShape::getSmallWidth(void)
{
    return width ;
}
void SmallShape::setSmallWidth ( double wid )
{
    width = wid;
}
int _tmain (int argc, _TCHAR* argv[])
{
  SmallShape Shape;
    // set Shape width using member function
   Shape.setSmallWidth(10.0);
   cout << "Width of Shape : "<< Shape.getSmallWidth() << endl;
    return 0;
   getchar ();
 }

The output of the above code is
Width of Shape is : 10


About the public, private and protected inheritance we can learn in the next posts.

No comments: