direct base classとindirect base classがある。
2つ以上のクラスを継承したクラスを多重継承(multiple inheritance)という
deriveは派生という意味
class Base1 { protected: int bs1; public: Base1(int b1=0) {bs1=b1;} void showBs1(); }; class Base2 { protected: int bs2; public: Base2(int b2=0) {bs2=b2;} void showBs2(); }; class Derived: public Base1, public Base2 { protected: int dr; public: Derived(int d=0){dr=d;} void showDr(); }; void Base1::showBs1() { cout << "bs1は" << bs1 << "です。\n"; } void Base2::showBs2() { cout << "bs2は" << bs2 << "です。\n"; } void Derived::showDr(){ cout << "drは" << dr << "です。\n"; } int main(){ Derived drv; drv.showBs1(); drv.showBs2(); drv.showDr(); return 0; }
メンバ関数が同じ場合
int main(){ Derived drv; drv.Base1::showBs(); drv.Base2::showBs(); drv.showDr(); return 0; }
仮想基本クラス(virtual base class)
class Base0 { protected: int bs0; public: Base0(int b0=0) {bs0=b0;} void showBs0(); }; class Base1 : public virtual Base0{ protected: int bs1; public: Base1(int b1=0) {bs1=b1;} void showBs(); }; class Base2 : public virtual Base0{ protected: int bs2; public: Base2(int b2=0) {bs2=b2;} void showBs(); }; class Derived: public Base1, public Base2 { protected: int dr; public: Derived(int d=0){dr=d;} void showDr(); }; void Base0::showBs0() { cout << "bs0は" << bs0 << "です。\n"; } void Base1::showBs() { cout << "bs1は" << bs1 << "です。\n"; } void Base2::showBs() { cout << "bs2は" << bs2 << "です。\n"; } void Derived::showDr(){ cout << "drは" << dr << "です。\n"; } int main(){ Derived drv; drv.showBs0(); return 0; }