目次
1. 継承の概要
もともと存在していたものは基本クラスであり、親クラスとも呼ばれます。既存のデータ型を使用して定義された新しいデータ型は派生クラスと呼ばれ、サブクラスとも呼ばれます。
基本クラスはクラスのすべてのサブクラスに共通のメンバーを定義し、派生クラスは各クラスに固有のメンバーを定義します。
C++ の派生クラスには、public、protect、private の 3 つの異なる継承方法があります。(デフォルトはプライベートです)
単一継承
単一継承の形式は、同じ基本クラスからサブクラスを派生することです。
class Father{};
class Son1:public Father{}; // public 方式继承
class Son2 :protected Father{}; // protected 方式继承
class Son3 :private Father{}; // private 方式继承
多重継承
多重継承は、複数の基本クラスから派生する派生クラスです。
class Base1{};
class Base2{};
class SubMulBase :public Base1, public Base2{};
2. 継承方法
2.1 派生クラスの構成
派生クラスは 2 つの部分で構成されます。1 つは親クラスから継承された属性で、もう 1 つは現在のクラスに追加された属性です。
class Base
{
public:
int m_A;
};
class SubBase :public Base
{
public :
int m_B;
private:
int m_C;
};
2.2 派生クラスの継承方法
文法:
class 子类: 继承方式 父类
継承方法には次の 3 つがあります。
- パブリック継承
- 保護された継承
- プライベート継承:
派生した派生クラス内の継承された各属性のスコープ (メソッドにも同じことが当てはまります)。
class Father
{
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son:protectedFather
{
};
すると、このときのサブクラスの属性は実際には次のようになります(プライベートなものにはアクセスできません)。
class Son:protectedFather
{
protected:
int m_A;
int m_B;
};
3. 継承におけるオブジェクトモデル
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C; //私有成员只是被隐藏了,但是还是会继承下去
};
//公共继承
class Son :public Base
{
public:
int m_D;
};
void test01()
{
cout << "sizeof Son = " << sizeof(Son) << endl;
}
出力結果:
4. 相続における建設と破壊の順序
class Base
{
public:
Base()
{
cout << "Base构造函数!" << endl;
}
~Base()
{
cout << "Base析构函数!" << endl;
}
};
class Son : public Base
{
public:
Son()
{
cout << "Son构造函数!" << endl;
}
~Son()
{
cout << "Son析构函数!" << endl;
}
};
5. 同名処理メソッドを継承
- 同じ名前のサブクラスのメンバーにアクセスするには、直接アクセスできます。
- 同じ名前の親クラスのメンバーにアクセスするには、スコープを追加する必要があります
class Base {
public:
Base()
{
m_A = 100;
}
void func()
{
cout << "Base - func()调用" << endl;
}
void func(int a)
{
cout << "Base - func(int a)调用" << endl;
}
public:
int m_A;
};
class Son : public Base {
public:
Son()
{
m_A = 200;
}
//当子类与父类拥有同名的成员函数,子类会隐藏父类中所有版本的同名成员函数
//如果想访问父类中被隐藏的同名成员函数,需要加父类的作用域
void func()
{
cout << "Son - func()调用" << endl;
}
public:
int m_A;
};
void test01()
{
Son s;
cout << "Son下的m_A = " << s.m_A << endl;
cout << "Base下的m_A = " << s.Base::m_A << endl;
s.func();
s.Base::func();
s.Base::func(10);
}
int main() {
test01();
system("pause");
return EXIT_SUCCESS;
}
要約:
- サブクラス オブジェクトは、サブクラス内の同じ名前のメンバーに直接アクセスできます。
- サブクラス オブジェクトとスコープは、同じ名前の親クラスのメンバーにアクセスできます
- サブクラスと親クラスに同名のメンバ関数がある場合、サブクラスは親クラスの同名のメンバ関数を非表示にし、スコープを追加することで親クラスの同名のメンバ関数にアクセスできるようになります。
5.2 静的メンバー
- 同じ名前のサブクラスのメンバーにアクセスするには、直接アクセスできます。
- 同じ名前の親クラスのメンバーにアクセスするには、スコープを追加する必要があります
class Base {
public:
static void func()
{
cout << "Base - static void func()" << endl;
}
static void func(int a)
{
cout << "Base - static void func(int a)" << endl;
}
static int m_A;
};
int Base::m_A = 100;
class Son : public Base {
public:
static void func()
{
cout << "Son - static void func()" << endl;
}
static int m_A;
};
int Son::m_A = 200;
//同名成员属性
void test01()
{
//通过对象访问
cout << "通过对象访问: " << endl;
Son s;
cout << "Son 下 m_A = " << s.m_A << endl;
cout << "Base 下 m_A = " << s.Base::m_A << endl;
//通过类名访问
cout << "通过类名访问: " << endl;
cout << "Son 下 m_A = " << Son::m_A << endl;
cout << "Base 下 m_A = " << Son::Base::m_A << endl;
}
//同名成员函数
void test02()
{
//通过对象访问
cout << "通过对象访问: " << endl;
Son s;
s.func();
s.Base::func();
cout << "通过类名访问: " << endl;
Son::func();
Son::Base::func();
//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作作用域访问
Son::Base::func(100);
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
6. 多重継承
構文: クラス サブクラス: 継承メソッドの親クラス 1、継承メソッドの親クラス 2...
多重継承では、親クラスに同じ名前のメンバーが現れる可能性があり、スコープの区別が必要です。
多重継承では、親クラスに同じ名前のメンバーが現れる場合、サブクラスを使用するときにスコープを追加する必要があります。