C++多继承

//c++里的单继承

#include <iostream>


using namespace std;

class father
{
private:
    int a;
    int b;
public:
    int c;
    int d;
protected:
    int e;
    int*p;

public:
    void setA(int newA)
    {
        a=newA;
    };
    void showA(void)
    {
        cout<<a<<"father show A"<<endl;
    }
};

class mother
{
private:
    int a;
public:
    void setA(int newA)
    {
        a=newA;
    }
    void showA(void)

    {
        cout<<a<<"mother show A"<<endl;
    }
};

//默认是私有继承
//多继承
class Son:public father,public mother
{
public:

    void func(void)
    {
        c=9;
        e=10;
    };

private:
    int A;

    void fuc11(void)
    {
        father::c=10;
    };

};
int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";

    Son son;
    son.father::setA(9999);
    son.father::showA();
    cout<<endl;
    son.mother::showA();
    //继承于父类的方法,可以访问父类的私有变量
    //继承是怎么发生的呢?
    //所谓的继承,其实是继承了父类的所有变量,之时编译器做了权限设置,
    //父类访问父类的变量是是给前四个字节,
    //继承的子类也能访问前四个字节
    //子类(父类的空间+自己独有的空间,)父类的空间在前,子类在后

    cout<<sizeof(father)<<endl;
    cout<<sizeof(son)<<endl;
    //子类的空间要比父类的空间大
    return 0;
}
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值