C++ 類訪問修飾符



C++ 訪問修飾符用於資料隱藏實現。資料隱藏是面向物件程式設計的重要特性之一,它允許程式的函式直接訪問類型別的內部表示。對類成員的訪問限制由類體內的標記為public、privateprotected的部分指定。關鍵字 public、private 和 protected 稱為訪問說明符。

一個類可以有多個 public、protected 或 private 標記的部分。每個部分都保持有效,直到遇到另一個部分標籤或類體結束的右括號。成員和類的預設訪問許可權為 private。

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

公共訪問修飾符

public 訪問修飾符定義公共資料成員和成員函式,這些成員函式可以在類外部但在程式內部的任何地方訪問。您可以無需任何成員函式即可設定和獲取公共變數的值。

示例

以下示例演示了 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 訪問修飾符定義私有資料成員和成員函式,這些成員函式無法從類外部訪問甚至檢視。只有類和友元函式可以訪問私有成員。

預設情況下,類的所有成員都是私有的,例如在以下類中,width 是一個私有成員,這意味著在您標記成員之前,它將被假定為私有成員。

示例

以下示例演示了 private 訪問修飾符的使用:

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

實際上,我們在 private 部分定義資料,在 public 部分定義相關函式,以便可以從類外部呼叫它們,如下面的程式所示。

#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
cpp_classes_objects.htm
廣告