Cplusplus-cpp-class-access-modifiers

提供:Dev Guides
移動先:案内検索

C ++クラスアクセス修飾子

データの非表示は、オブジェクト指向プログラミングの重要な機能の1つであり、プログラムの機能がクラス型の内部表現に直接アクセスできないようにします。 クラスメンバへのアクセス制限は、クラス本体内のラベル付きの* 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* メンバー変数または関数にアクセスしたり、クラスの外部から表示することさえできません。 クラスおよびフレンド機能のみがプライベートメンバーにアクセスできます。

デフォルトでは、クラスのすべてのメンバーはプライベートになります。たとえば、次のクラスでは 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* メンバー変数または関数はプライベートメンバーに非常に似ていますが、派生クラスと呼ばれる子クラスでアクセスできるというもう1つの利点があります。

次の章で、派生クラスと継承について学習します。 今のところ、親クラス Box から1つの子クラス 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