[C++] 繼承整理

繼承的分類:

1. 公開繼承(public): 子成員只能使用父類別中宣告為 public/protected 的函式(function)與成員(member),但是不能夠

class A{
    public:
        int one;
    protected:
        int two;
    private:
        int three;
};

class B: public A{
    public:
    void setvalue(){
        one = 1; // OK
        two = 2; // OK
        three = 3; // NG
    }
}

class C: private A{
    public:
    void setvalue(){
        one = 1;   // OK
        two = 2;   // OK
        three = 3; // OK
    }
}

void main(){
    B b;
    b.one = 1;  // OK
    b.two = 2;  // NG
    b.three = 3;// NG
    C c;
    c.one = 1;  // OK
    c.two = 2;  // NG
    c.three = 3;// NG
}

虛擬繼承(Virtual Inheritance):當一個 class 繼承多的 parent class,而這些父類別又繼承同一個上層的父類別時,虛擬繼承需要被用到,以下為一個簡單的範例。

class A{
    //     
};

class B1: virtual public A{
    //
};
 
class B2: virtual public A{
    //
};

class C: public B1, public B2{
    //
};