目录

类访问修饰符(Class Access Modifiers)

数据隐藏是面向对象编程的重要特征之一,它允许阻止程序的功能直接访问类类型的内部表示。 对类成员的访问限制由类主体中标记的public, private,protected部分指定。 关键字public,private和protected称为访问说明符。

一个类可以有多个公共,受保护或私有标记的部分。 每个部分保持有效,直到看到另一个部分标签或类主体的右侧右支撑。 成员和类的默认访问权限是私有的。

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

公众成员

public成员可以从课外的任何地方访问,但可以在程序中访问。 您可以设置和获取没有任何成员函数的公共变量的值,如以下示例所示 -

#include <iostream>
using namespace std;
class Line {
   public:
      double length;
      void setLength( double len );
      double getLength( void );
};
// Member functions definitions
double Line::getLength(void) {
   return length ;
}
void Line::setLength( double len) {
   length = len;
}
// Main function for the program
int main() {
   Line line;
   // set line length
   line.setLength(6.0); 
   cout << "Length of line : " << line.getLength() <<endl;
   // set line length without member function
   line.length = 10.0; // OK: because length is public
   cout << "Length of line : " << line.length <<endl;
   return 0;
}

编译并执行上述代码时,会产生以下结果 -

Length of line : 6
Length of line : 10

私人会员

无法访问private成员变量或函数,甚至无法从类外部查看。 只有class和friend函数才能访问私有成员。

默认情况下,类的所有成员都是私有的,例如在下面的类中, width是私有成员,这意味着在标记成员之前,它将被假定为私有成员 -

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

实际上,我们在私有部分中定义数据,在公共部分中定义相关函数,以便可以从类外部调用它们,如以下程序所示。

#include <iostream>
using namespace std;
class Box {
   public:
      double length;
      void setWidth( double wid );
      double getWidth( void );
   private:
      double width;
};
// Member functions definitions
double Box::getWidth(void) {
   return width ;
}
void Box::setWidth( double wid ) {
   width = wid;
}
// Main function for the program
int main() {
   Box box;
   // set box length without member function
   box.length = 10.0; // OK: because length is public
   cout << "Length of box : " << box.length <<endl;
   // set box width without member function
   // box.width = 10.0; // Error: because width is private
   box.setWidth(10.0);  // Use member function to set it.
   cout << "Width of box : " << box.getWidth() <<endl;
   return 0;
}

编译并执行上述代码时,会产生以下结果 -

Length of box : 10
Width of box : 10

受保护的会员

protected成员变量或函数与私有成员非常相似,但它提供了一个额外的好处,即可以在称为派生类的子类中访问它们。

您将在下一章学习派生类和继承。 现在,您可以查看以下示例,其中我从父类Box派生了一个子类SmallBox

下面的示例与上面的示例类似,此处的width成员可以通过其派生类SmallBox的任何成员函数访问。

#include <iostream>
using namespace std;
class Box {
   protected:
      double width;
};
class SmallBox:Box { // SmallBox is the derived class.
   public:
      void setSmallWidth( double wid );
      double getSmallWidth( void );
};
// Member functions of child class
double SmallBox::getSmallWidth(void) {
   return width ;
}
void SmallBox::setSmallWidth( double wid ) {
   width = wid;
}
// Main function for the program
int main() {
   SmallBox box;
   // set box width using member function
   box.setSmallWidth(5.0);
   cout << "Width of box : "<< box.getSmallWidth() << endl;
   return 0;
}

编译并执行上述代码时,会产生以下结果 -

Width of box : 5
↑回到顶部↑
WIKI教程 @2018