C++:クラス、クラスアクセス修飾子
目次
クラス#クラス#
クラスアクセス修飾子
パブリックメンバー
プライベートメンバー
保護(protected)メンバー
クラス#クラス#
クラス定義はclassで始まる
//定義Boxクラスclass Box{public://共通データメンバーはクラスの外部でdouble length;double breadth;double height; };
クラスアクセス修飾子
デフォルトでは、クラスのすべてのメンバーはプライベートです.
class Base{public://共有メンバーprotected://保護メンバーprivate://プライベートメンバー};
パブリックメンバー
共有メンバーは、プログラム内のクラスの外部で(.)を使用してアクセスします.共有変数の値を設定および取得するには、メンバー関数を使用しません.
プライベートメンバー
プライベートメンバー変数または関数は、クラスの外部ではアクセスできません.確認できません.プライベートメンバーにアクセスできるのは、クラスと友元関数のみです.
(1)クラスおよびクラスのメンバー変数の定義
(2)実際の操作では,一般にプライベート領域でデータを定義し,共通領域で関連する関数を定義し,クラスの外部でもこれらの関数を呼び出すことができるようにする.
(3)クラスの外部でプライベート変数にアクセスできるように、メンバー関数を定義する
(4)主関数へのアクセス
保護(protected)メンバー
保護メンバー変数または関数は、プライベートメンバーとよく似ていますが、派生クラス(サブクラス)で保護メンバーがアクセスできる点が異なります.
(1)クラスとその派生クラスの定義
P.s.:ここではクラスの継承に関し、デフォルトprivate継承で、ベースクラスのprotectedメンバーのアクセス属性が派生クラスでprivateになります.
(2)サブクラスのメンバー関数の設定
(3)主関数でのアクセス
参考資料:
https://www.runoob.com/cplusplus/cpp-tutorial.html
クラス#クラス#
クラスアクセス修飾子
パブリックメンバー
プライベートメンバー
保護(protected)メンバー
クラス#クラス#
クラス定義はclassで始まる
//定義Boxクラスclass Box{public://共通データメンバーはクラスの外部でdouble length;double breadth;double height; };
クラスアクセス修飾子
デフォルトでは、クラスのすべてのメンバーはプライベートです.
class Base{public://共有メンバーprotected://保護メンバーprivate://プライベートメンバー};
パブリックメンバー
共有メンバーは、プログラム内のクラスの外部で(.)を使用してアクセスします.共有変数の値を設定および取得するには、メンバー関数を使用しません.
#include
using namespace std;
// Box
class Box
{
public: // (.)
double length;
double breadth;
double height;
};
int main( )
{
Box Box1; // Box1, Box
double volume = 0.0;
// Box 1
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// Box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Box1 :" << volume <
プライベートメンバー
プライベートメンバー変数または関数は、クラスの外部ではアクセスできません.確認できません.プライベートメンバーにアクセスできるのは、クラスと友元関数のみです.
(1)クラスおよびクラスのメンバー変数の定義
#include
using namespace std;
// Box
class Box
{
double length; //
public: // (.)
double breadth;
double height;
(2)実際の操作では,一般にプライベート領域でデータを定義し,共通領域で関連する関数を定義し,クラスの外部でもこれらの関数を呼び出すことができるようにする.
void setlength(double len); // length
double getlength(void); // length
};
(3)クラスの外部でプライベート変数にアクセスできるように、メンバー関数を定義する
//
void Box::setlength(double len)
{
// length
length = len;
}
double Box::getlength(void)
{
// length
return length;
}
(4)主関数へのアクセス
int main( )
{
Box Box1; // Box1, Box
double volume = 0.0;
// Box 1
// Box1.length = 6.0; // ,
Box1.setlength(6.0); // length
Box1.height = 5.0;
Box1.breadth = 7.0;
// Box 1
volume = Box1.getlength()*Box1.height*Box1.breadth; // getlength()
cout << "Box1 :" << volume <
保護(protected)メンバー
保護メンバー変数または関数は、プライベートメンバーとよく似ていますが、派生クラス(サブクラス)で保護メンバーがアクセスできる点が異なります.
(1)クラスとその派生クラスの定義
P.s.:ここではクラスの継承に関し、デフォルトprivate継承で、ベースクラスのprotectedメンバーのアクセス属性が派生クラスでprivateになります.
#include
using namespace std;
class Box
{
protected: // ,
double width;
};
class SmallBox:Box // SmallBox
{
public:
void setSmallWidth( double wid );
double getSmallWidth( void );
};
(2)サブクラスのメンバー関数の設定
//
double SmallBox::getSmallWidth(void)
{
return width ;
}
void SmallBox::setSmallWidth( double wid )
{
width = wid;
}
(3)主関数でのアクセス
//
int main( )
{
SmallBox box;
//
box.setSmallWidth(5.0);
cout << "Width of box : "<< box.getSmallWidth() << endl;
return 0;
}
参考資料:
https://www.runoob.com/cplusplus/cpp-tutorial.html