C++ 類訪問修飾符
 C++ 類 & 對象
數據封裝是面向對象編程的一個重要特點,它防止函數直接訪問類類型的內部成員。類成員的訪問限制是通過在類主體內部對各個區域標記 public、private、protected 來指定的。關鍵字 public、private、protected 稱為訪問修飾符。
一個類可以有多個 public、protected 或 private 標記區域。每個標記區域在下一個標記區域開始之前或者在遇到類主體結束右括弧之前都是有效的。成員和類的默認訪問修飾符是 private。
class Base {
   public:
  
   protected:
  
   private:
  
};
 
 
公有(public)成員
公有成員在程式中類的外部是可訪問的。您可以不使用任何成員函數來設置和獲取公有變數的值,如下所示:
實例
#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; 
   cout << "Length of line : " << line.length <<endl;
   return 0;
}
 
 
當上面的代碼被編譯和執行時,它會產生下列結果:
Length of line : 6
Length of line : 10
私有(private)成員
私有成員變數或函數在類的外部是不可訪問的,甚至是不可查看的。只有類和友元函數可以訪問私有成員。
默認情況下,類的所有成員都是私有的。例如在下面的類中,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;
};
double Box::getWidth(void)
{
    return width ;
}
void Box::setWidth( double wid )
{
    width = wid;
}
int main( )
{
   Box box;
   
   box.length = 10.0; 
   cout << "Length of box : " << box.length <<endl;
   
   
   box.setWidth(10.0);  
   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 
{
   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;
}
 
 
當上面的代碼被編譯和執行時,它會產生下列結果:
Width of box : 5
繼承中的特點
有public, protected, private三種繼承方式,它們相應地改變了基類成員的訪問屬性。
1.public 繼承:基類 public 成員,protected 成員,private 成員的訪問屬性在派生類中分別變成:public, protected, private
2.protected 繼承:基類 public 成員,protected 成員,private 成員的訪問屬性在派生類中分別變成:protected, protected, private
3.private 繼承:基類 public 成員,protected 成員,private 成員的訪問屬性在派生類中分別變成:private, private, private
但無論哪種繼承方式,上面兩點都沒有改變:
public 繼承
實例
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
  int a;
  A(){
    a1 = 1;
    a2 = 2;
    a3 = 3;
    a = 4;
  }
  void fun(){
    cout << a << endl;    
    cout << a1 << endl;   
    cout << a2 << endl;   
    cout << a3 << endl;   
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
class B : public A{
public:
  int a;
  B(int i){
    A();
    a = i;
  }
  void fun(){
    cout << a << endl;       
    cout << a1 << endl;       
    cout << a2 << endl;       
    cout << a3 << endl;       
  }
};
int main(){
  B b(10);
  cout << b.a << endl;
  cout << b.a1 << endl;   
  cout << b.a2 << endl;   
  cout << b.a3 << endl;   
  system("pause");
  return 0;
}
 
 
protected 繼承
實例
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
  int a;
  A(){
    a1 = 1;
    a2 = 2;
    a3 = 3;
    a = 4;
  }
  void fun(){
    cout << a << endl;    
    cout << a1 << endl;   
    cout << a2 << endl;   
    cout << a3 << endl;   
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
class B : protected A{
public:
  int a;
  B(int i){
    A();
    a = i;
  }
  void fun(){
    cout << a << endl;       
    cout << a1 << endl;       
    cout << a2 << endl;       
    cout << a3 << endl;       
  }
};
int main(){
  B b(10);
  cout << b.a << endl;       
  cout << b.a1 << endl;      
  cout << b.a2 << endl;      
  cout << b.a3 << endl;      
  system("pause");
  return 0;
}
 
 
private 繼承
實例
#include<iostream>
#include<assert.h>
using namespace std;
class A{
public:
  int a;
  A(){
    a1 = 1;
    a2 = 2;
    a3 = 3;
    a = 4;
  }
  void fun(){
    cout << a << endl;    
    cout << a1 << endl;   
    cout << a2 << endl;   
    cout << a3 << endl;   
  }
public:
  int a1;
protected:
  int a2;
private:
  int a3;
};
class B : private A{
public:
  int a;
  B(int i){
    A();
    a = i;
  }
  void fun(){
    cout << a << endl;       
    cout << a1 << endl;       
    cout << a2 << endl;       
    cout << a3 << endl;       
  }
};
int main(){
  B b(10);
  cout << b.a << endl;       
  cout << b.a1 << endl;      
  cout << b.a2 << endl;      
  cout << b.a3 << endl;      
  system("pause");
  return 0;
}
 
 
 C++ 類 & 對象