C++助教篇7_继承的十种常见情况

大家可能也快学到继承了,所以这里总结了一下C++继承的十种常见情况。还有一些情况,如菱形继承等没有提到。

以第一个为例,说明怎样 test 这些例子

普通公有派生

#include <iostream>
using namespace std;

class Base {
public:
    Base(int base_val = 0) : base(base_val) {}
    void output() const { cout << base; }

private:
    int base;
};

class Derived : public Base {
public:
    Derived(int base_val = 0, int derived_val = 0)
        : Base(base_val), derived(derived_val) {}
    void output() const { cout << derived; }

private:
    int derived;
};

int main(int argc, const char** argv) {
    Base bObj(1);
    Derived dObj(2, 3);
    cout << "The value of base is ";
    bObj.output();
    // cout << bObj.base;
    cout << endl << "the valuse of derived is ";
    dObj.output();
    // cout << dObj.derived;
    dObj.Base::output();
    // cout << dObj.base;
    system("pause");
    return 0;
}

没有虚析构函数,继承类没有虚构

class Base {
public:
    virtual void output() { cout << "Hello,I'm class Base"; }
    ~Base() { cout << "Oh,goodbye Base"; }
};

class Derived : public Base {
public:
    virtual void output() { cout << "Hello,I'm class Derived"; }
    ~Derived() { cout << "Oh,goodbye Derived"; }
};

纯虚函数,多态

class Base {
public:
    virtual void output() = 0;
};

class Derived : public Base {
public:
    void output() { cout << "Hello,I'm class Derived"; }
};

class AnotherDerived : public Base {
public:
    void output() { cout << "Hello,I'm AnotherDerived"; }
};

多继承

class Base {
public:
    Base(int base_val = 0) : base(base_val) {}
    void output() { cout << "Base: " << base; }

private:
    int base;
};

class AnotherBase {
public:
    AnotherBase(int ab_val = 0) : another_base(ab_val) {}
    void output() { cout << "AnotherBase: " << another_base; }

private:
    int another_base;
};

class Derived : public Base, public AnotherBase {
public:
    Derived(int base_val, int ab_val, int d_val)
        : Base(base_val), AnotherBase(ab_val), derived(d_val) {}
    void output() { cout << "Derived: " << derived; }

private:
    int derived;
};

共同基类的多继承

class Father {
public:
    Father(int fval) : father(fval) {}
    void output() { cout << "Father: " << father; }

private:
    int father;
};

class Son : public Father {
public:
    Son(int fval, int sval) : Father(fval), son(sval) {}
    void output() { cout << "Son: " << son; }

private:
    int son;
};

class Daughter : public Father {
public:
    Daughter(int fval, int dval) : Father(fval), daughter(dval) {}
    void output() { cout << "Daughter: " << daughter; }

private:
    int daughter;
};

class GrandSon : public Son, public Daughter {
public:
    GrandSon(int fval, int sdval, int gval)
        : Son(fval, sdval), Daughter(fval, sdval), grandSon(gval) {}
    void output() { cout << "GrandSon: " << grandSon; }

private:
    int grandSon;
};

虚基类

class Base {
public:
    void output() { cout << "Base"; }
};

class SonA : public virtual Base {
public:
    void output() {
        cout << "SonA" << endl;
        Base::output();
    }
};

class SonB : public virtual Base {
public:
    void output() {
        cout << "SonB" << endl;
        Base::output();
    }
};

class GrandSon : public SonA, public SonB {
public:
    void output() {
        cout << "GrandSon" << endl;
        SonA::output();
        SonB::output();
    }
};

私有继承

class Base {
public:
    Base(int publ_val, int prot_val, int priv_val)
        : publ(publ_val), prot(prot_val), priv(priv_val) {}
    int getPubl() const { return publ; }

protected:
    int getProt() const { return prot; }

private:
    int getPriv() const { return priv; }

public:
    int publ;

protected:
    int prot;

private:
    int priv;
};

class Derived : private Base {
public:
    Derived(int publ_val, int prot_val, int priv_val)
        : Base(publ_val, prot_val, priv_val) {}
    int get_publ() const { return getPubl(); }
    int get_prot() const { return getProt(); }
    int get_priv() const { return getPriv(); }  // not accessible
};

多级单线继承

多级继承的常见的还有菱形继承,但是不推荐那样设计。

class Tiger {
public:
    Tiger(int age_num) : age(age_num) {}
    void roar() const { cout << "Tiger is roaring..."; }

protected:
    int age;
};

class Female_Tiger : public Tiger {
public:
    Female_Tiger(int age_num, string sex_opt) : Tiger(age_num), sex(sex_opt) {}
    void roar() const { cout << "Female_Tiger is roaring..."; }

protected: // can be accessed by derived class
    string sex;
};

class Small_Female_Tiger : public Female_Tiger {
public:
    Small_Female_Tiger(int age_num, string sex_opt, string partent_name)
        : Female_Tiger(age_num, sex_opt), partent(partent_name) {}
    void roar() const { cout << "Small_Female_Tiger is roaring..."; }

protected:
    string partent;
};

类的兼容性原则

class Base {
public:
    void output() { cout << "Base" << endl; }
};

class Derived : public Base {
public:
    void output() { cout << "Derived" << endl; }
};

void test_function(Base base) { base.output(); }

int main(int argc, const char **argv) {
    // 注意观察下面输出的值,看是否和你的猜测一样
    Derived derived;
    Base base;

    Base *ptrBase = &derived;
    Base &refBase = derived;
    base = derived;

    base.output();
    derived.output();
    ptrBase->output();
    test_function(derived);
    refBase.output();

    system("pause");
    return 0;
}

虚析构函数,防止内存泄漏

查看Base类加virtual前后创建一个对象析构的输出有什么区别

class Base {
public:
    // ~Base(){cout<<"Base Destructor";}
    virtual ~Base(){cout<<"Base Destructor";}
};

class Derived : public Base {
public:
    ~Derived(){cout<<"Derived Constructor";}
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值