Latest web development tutorials

C ++ class access modifier

C ++ Class & Objects C ++ Class & Objects

Hidden data object-oriented programming is an important feature, which prevents direct access to the internal member function of a class type. Access is restricted by the class members inside the main body of each region markedpublic, private, protected to specify.Keywords public, private, protected called access specifier.

A class can have more than one public, protected or private branded zone. Before each labeled zone marked area next start or the end of the class body before meeting the right parenthesis are valid. The default access modifiers and class members are private.

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

Public (public) member

Public member in the program outside the class are accessible.You can not use any member functions to set and get the value of public variables, as follows:

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      double length;
      void setLength( double len );
      double getLength( void );
};
 
// 成员函数定义
double Line::getLength(void)
{
    return length ;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
// 程序的主函数
int main( )
{
   Line line;
 
   // 设置长度
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
 
   // 不使用成员函数设置长度
   line.length = 10.0; // OK: 因为 length 是公有的
   cout << "Length of line : " << line.length <<endl;
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Length of line : 6
Length of line : 10

Private (private) member

Private member variables or functions outside the class is not accessible, even impossible to see.Only classes and friend function can access private members.

By default, all members of the class are private. For example, in the followingclass, width is a private members, which means that if you do not use any access modifiers, class members will be assumed to be private members:

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

In practice, we generally define the data in the private area, as defined in the relevant area of ​​public functions to outside the class can also invoke these functions as follows:

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );
 
   private:
      double width;
};
 
// 成员函数定义
double Box::getWidth(void)
{
    return width ;
}
 
void Box::setWidth( double wid )
{
    width = wid;
}
 
// 程序的主函数
int main( )
{
   Box box;
 
   // 不使用成员函数设置长度
   box.length = 10.0; // OK: 因为 length 是公有的
   cout << "Length of box : " << box.length <<endl;
 
   // 不使用成员函数设置宽度
   // box.width = 10.0; // Error: 因为 width 是私有的
   box.setWidth(10.0);  // 使用成员函数设置宽度
   cout << "Width of box : " << box.getWidth() <<endl;
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Length of box : 10
Width of box : 10

Protection (protected) members

Protected member variables or functions with private members is very similar, but a little different, to protect members of the derived class (ie, subclasses) are accessible.

In the next section, you will learn the knowledge derived classes and inheritance. Now you can see the following examples, we derive from the parent classBox A subclass smallBox.

The following example is similar to the previous examples, where thewidth of any member may be a derived class member function smallBox access.

#include <iostream>
using namespace std;
 
class Box
{
   protected:
      double width;
};
 
class SmallBox:Box // SmallBox 是派生类
{
   public:
      void setSmallWidth( double wid );
      double getSmallWidth( void );
};
 
// 子类的成员函数
double SmallBox::getSmallWidth(void)
{
    return width ;
}
 
void SmallBox::setSmallWidth( double wid )
{
    width = wid;
}
 
// 程序的主函数
int main( )
{
   SmallBox box;
 
   // 使用成员函数设置宽度
   box.setSmallWidth(5.0);
   cout << "Width of box : "<< box.getSmallWidth() << endl;
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Width of box : 5

C ++ Class & Objects C ++ Class & Objects