|
若干間が空きましたが 継承を一言で表現することはできませんが 多様性については説明するよりも 今回は継承による とりあえず、継承クラスの書き方から・・・ Parentクラスを親に持つ class Child:public Parent
{
};
と書きます クラスのpublicやprivateのキーワードについては わざわざ、アクセスのキーワードをつけるのは まず、アクセスキーワードによってどこからアクセスできるのかを見てみましょう // クラスの継承
#include <iostream>
using namespace std;
// 基本クラス(Childの親クラス)
class Parent
{
// private部は自分のクラス外では一切使えない
private:
int a; // Parentクラス内のみで使える変数
void TestPrivate() // Parentクラス内のみで使える関数
{
cout << "親クラスprivate関数" << endl;
};
// protected部は継承クラスでも使える
protected:
int b;
void TestProtected()
{
cout << "親クラスprotected関数" << endl;
};
// public部はさらに外部(main)からでも使える(どこからでも使える)
public:
int c;
// コンストラクタ
Parent(){}
// クラス内変数を引数付コンストラクタで初期化
Parent(int inputA,int inputB,int inputC)
:a(inputA),b(inputB),c(inputC){}
void ShowParent()
{
TestPrivate();
TestProtected();
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
};
// 派生クラス(Parentの継承クラス)
class Child: public Parent// ←public継承
{
private:
int d;
public:
int e;
// クラス内変数をコンストラクタで初期化
Child(int inputA,int inputB,int inputC,int inputD,int inputE)
:Parent(inputA,inputB,inputC),d(inputD),e(inputE){}
void ShowChild()
{
//TestPrivate(); // 親クラスのprivateにはアクセス不可
TestProtected(); // 親クラスがpublicかprotectedならばアクセス可能
//cout << "a = " << a << endl; // 親クラスのprivateにはアクセス不可
cout << "b = " << b << endl; // 親クラスがpublicかprotectedならばアクセス可能
cout << "c = " << c << endl; // 親クラスがpublicかprotectedならばアクセス可能
cout << "d = " << d << endl;
cout << "e = " << e << endl;
}
};
// main関数(クラスの外部)
int main()
{
int inputA = 1,inputB = 2,inputC = 3,inputD = 4,inputE = 5;
Parent p(inputA,inputB,inputC);
//cout << "a = " << p.a << endl;// privateは外部からアクセス不可
//cout << "b = " << p.b << endl;// protectedは外部からアクセス不可
cout << "c = "<< p.c << endl; // publicなので外部からアクセス可能
//p.TestPrivate(); // privateは外部からアクセス不可
//p.TestProtected(); // protectedは外部からアクセス不可
p.ShowParent(); // publicなので外部からアクセス可能
cout << endl << endl;
Child ch(inputA,inputB,inputC,inputD,inputE);
//cout << "a = " << ch.a << endl;// privateは外部からアクセス不可
//cout << "b = " << ch.b << endl;// protectedは外部からアクセス不可
cout << "c = "<< ch.c << endl; // 親クラスのpublicなので外部からアクセス可能
//cout << "d = "<< ch.d << endl;// 子クラスのprivateなので外部からアクセス不可
cout << "e = "<< ch.e << endl; // 子クラスのpublicなので外部からアクセス可能
//ch.TestPrivate(); // privateは外部からアクセス不可
//ch.TestProtected(); // protectedは外部からアクセス不可
ch.ShowParent(); // 親クラスのpublicなので外部からアクセス可能
ch.ShowChild(); // 子クラスのpublicなので外部からアクセス可能
return 0;
}
余力があったら 直感としてはpublicはどこからでもアクセス可能 と覚えておけばおk さて、public継承とprotected継承の違いについて説明します class Child: public Parent// ←public継承 次のようにprotected継承にします class Child: protected Parent// ←protected継承 すると↓の部分でエラーがでると思います cout << "c = "<< ch.c << endl; // 親クラスのpublicなので外部からアクセス可能 ch.ShowParent(); // 親クラスのpublicなので外部からアクセス可能 上の2行をコメントアウトすると privateな継承というものも存在しますが
|