C++类的继承

c++类的继承


经过编写代码仔细尝试,我总结出这么两条规则:
这里写图片描述

  • 补充
    • 函数重写分两种情况
      • 派生类重写基类的虚函数
        • 在拷贝的那份上重写
      • 派生类重写基类的普通函数
        • 参考拷贝的那份又重新写了一份

然后还有一张速记表
这里写图片描述

  • 这里的 成员访问不可见成员,意思是,即使在定义方法(此时还是在类里),也不能直接访问的成员,这种被彻底隐藏的成员需要调用基类的读写器(get/set)才能进行操做。或者在设计基类时就把之后会用到成员放在protected里,这样既安全,子类用起来又方便。

  • 多继承

    • 儿子有多个爸爸
    • 并联
    • 构造执行顺序
      • 先基类,后派生
      • 先做边,后右边
    • 析构执行顺序
      • 和构造正好逆过来
  • 多重继承
    • 爷、父、子三代同堂
    • 串联
    • 成员按照两个要点规则继承
    • 构造执行顺序
      • 先晚辈,后小辈
    • 析构执行顺序
      • 和构造正好逆过来

下面是我实验的代码

#include <iostream>
using namespace std;
class test
{
private:
    int i = 10;//一、对本类研究
    void funi(void)//验证各自权限下的函数
    {
        cout << j << endl;//访问其它权限里的变量
        cout << k << endl;
        pj();//访问其他权限里的函数
        pk();
    }//经验证:在类里三种权限可互相访问(类里可以无视权限修饰符)
    void pi(void)
    {
        cout << "i" << endl;
    }
protected:
    int j = 20; 
    void funj(void)
    {
        cout << i << endl;
        cout << k << endl;
        pi();
        pk();
    }
    void pj(void)
    {
        cout << "j" << endl;
    }
public:
    int k = 30;
    void funk(void)
    {
        cout << i << endl;
        cout << j << endl;
        pi();
        pj();
        funi();
        funj();
    }
    void pk(void)
    {
        cout << "k" << endl;
    }
};
class A:public test
{
private:
    int x = 11;//二、研究父子类
    void funx(void)//验证子类三种权限里新函数
    {
        //cout << i << endl;//访问父类三种权限里的变量
        cout << j << endl;
        cout << k << endl;
        //funi();//访问父类三种权限里的函数
        funj();
        funk();
    }
protected:
    int y = 21;
    void funy(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
public:
    int z = 31;
    void funz(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
};
class B:protected test
{
private:
    int o = 12;
    void funo(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
protected:
    int p = 22;
    void funp(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
public:
    int q = 32;
    void funq(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
};
class C:private test
{
private:
    int m = 13;
    void funm(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
protected:
    int n = 23;
    void funn(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
public:
    int w = 33;
    void funw(void)
    {
        //cout << i << endl;
        cout << j << endl;
        cout << k << endl;
        //funi();
        funj();
        funk();
    }
};
class D:test//和private一样
{
private:
    int a = 14;
protected:
    int b = 24;
public:
    int c = 34;
};
int main(void)
{
    cout << "=======================" << endl;
    test t = test();//三、验证父类对象
//  cout << t.i << endl;//访问本类三种权限里的变量
//  cout << t.j << endl;
    cout << t.k << endl;
//  t.funi();//访问本类三种权限里的函数
//  t.funj();
    t.funk();
    cout << "=======================" << endl;

    A a = A();  //四、验证子类对象
//  cout << a.i << endl;//访问父类三种权限里的变量
//  cout << a.j << endl;
    cout << a.k << endl;
    //a.funi();//访问父类三种权限里的函数
    //a.funj();
    a.funk();
    cout << "=======================" << endl;

    B b = B();
//  cout << b.i << endl;
//  cout << b.j << endl;
//  cout << b.k << endl;
//  b.funi();
//  b.funj();
//  b.funk();
    cout << "=======================" << endl;

    C c = C();
//  cout << c.i << endl;
//  cout << c.j << endl;
//  cout << c.k << endl;
//  c.funi();
//  c.funj();
//  c.funk();
    cout << "=======================" << endl;
    return 0;
}   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值