• C++核心编程(十九)继承的方式


    首先复习一下:
    类中成员可以分为3种:
    1)公有成员:可以在类内访问,也可以在类外访问(通过对象访问),子类和本类都可以访问;
    2)保护成员:不可以类外访问,可以在子类或本类中访问
    3)私有成员:不可以类外访问,不可以子类中访问,不可以对象访问,只能本类中访问;

    (1)继承的方式:
    1)公有继承
    2)保护继承继承
    3)私有继承

    1)公有继承
    1----子类成员可以访问父类的公有成员和保护成员
    2 ----子类对象可以访问父类的公有成员
    3----子类无法访问父类的私有成员
    4----子类的子类可以访问父类的公有成员和保护成员(具体访问权限需要看子类和子类的子类的继承关系)
    5----子类的子类无法访问父类的的私有成员
    公有继承的例子如下:

    #include<iostream>
    using namespace std;
    class father
    {
    public:
            void father_public();
    protected:
            void father_protected(); 
    private:
            void father_private();
    };
    
    void father::father_public()
    {
             cout<<"this is father_public"<<endl;
    }
    
    void father::father_protected()
    {
            cout<<"this is father_protected"<<endl;
    }
    void father::father_private()
    {
             cout<<"this is father_private"<<endl;
    }
    //类的公有继承。
    //通过子类对象可以直接访问父类的公有成员;
    //可以在子类中直接访问父类的保护成员,但是不能通过子类的对象访问父类的保护成员;
    //子类不能在子类中直接访问父类的私有成员,
    class son:public father
    {
    public:
            void son_call_on_protected();
    };
    
    void son::son_call_on_protected()
    {
        cout<<"son_son_call_on_public ";
        father_protected(); //可以访问father中的protected;
                            //father_private();仍然无法访问
    }
    
    void test_public_inherrit()
    {
        father  father_obj;
                // father_obj.father_protected(); protected修饰的成员不能再外部访问,只能在类中访问
    
            son son_obj;
            son_obj.father_public();
                //    obj.father_protected();保护成员只能在本类和子类中被访问,不能在外部被访问
                //     son_obj.father_private();父类的私有成员子类无法直接访问,父类的私有成员只能被父类的成员访问。
            son_obj.son_call_on_protected();
    }
    
    int main()
    {
       test_public_inherrit();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    2)保护继承
    1----子类对象无法访问父类
    2----子类成员可以访问父类的公有成员和保护成员
    3----子类无法访问父类的私有成员 (无论是成员访问还是对象访问都不行)
    4----子类的子类可以通过子类的子类的成员访问父类的公有成员和保护成员
    5----子类的子类无法访问父类的的私有成员 (无论是成员访问还是对象访问都不行)
    例子:

    #include<iostream>
    using namespace std;
    class father
    {
    public:
            void father_public();
    protected:
            void father_protected();
    private:
            void father_private();
    };
    void father::father_public()
    {
            cout<<"father_public"<<endl;
    }
    
    void father::father_protected()
    {
            cout<<"father_protected"<<endl;
    }
    
    void father::father::father_private()
    {
            cout<<"father_private"<<endl;
    }
    
    class son:protected father//保护继承
    {
    public:
            void s_call_on_f_public_and_protected();
    };
    
    void son::s_call_on_f_public_and_protected()
    {
            cout<<"this is son"<<endl;
            father_public();
            father_protected();
           // father_private();报错:在son类中访问father的私有成员
    }
    
    class grandson:public son
    {
    public:
            void g_call_on_f_public_and_protected();
    };
    
    void grandson::g_call_on_f_public_and_protected()
    {
            cout<<"this is grandson"<<endl;
            father_public();
            father_protected();
           // father_private();报错:在grandson类中访问father的私有成员
    }
    int main()
    {
            son son_obj;
                 son_obj.s_call_on_f_public_and_protected();
            grandson g_son_obj;
                 g_son_obj.g_call_on_f_public_and_protected();
            // son_obj.father_public();报错:通过对象访问father的公有成员
            //son_obj.father_protected();报错:通过对象访问father的保护成员
           // father_private();//报错:通过对象访问father的私有成员
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    3)私有继承
    1----子类的成员可以访问父类的公有成员和保护成员
    2----子类的对象不能访问父类的公有成员和保护成员(无论是成员访问还是对象访问都不行)
    3----子类的子类不能访问子类的父类的成员,(无论是成员访问还是对象访问都不行)

    例子:

    #include<iostream>
    using namespace std;
    class father
    {
    public:
            void father_public();
    protected:
            void father_protected();
    private:
            void father_private();
    };
    void father::father_public()
    {
            cout<<"father_public"<<endl;
    }
    
    void father::father_protected()
    {
            cout<<"father_protected"<<endl;
    }
    
    void father::father::father_private()
    {
            cout<<"father_private"<<endl;
    }
    
    class son:private father//私有继承
    {
    public:
            void s_call_on_f_public_and_protected();
    };
    
    void son::s_call_on_f_public_and_protected()
    {
            cout<<"this is son"<<endl;
            father_public();
            father_protected();
           // father_private();报错:在son类中访问father的私有成员
    }
    
    class grandson:public son
    {
    public:
            void g_call_on_f_public_and_protected();
    };
    
    void grandson::g_call_on_f_public_and_protected()
    {
            cout<<"this is grandson"<<endl;
            //  father_public();报错:在grandson类中访问father的公有成员
            // father_protected();报错:在grandson类中访问father的保护成员
           // father_private();报错:在grandson类中访问father的私有成员
    }
    int main()
    {
            son son_obj;
                 son_obj.s_call_on_f_public_and_protected();
            // son_obj.father_public();报错:通过对象访问father的公有成员
            //son_obj.father_protected();报错:通过对象访问father的保护成员
           // father_private();//报错:通过对象访问father的私有成员
           grandson g_son_obj;
                 g_son_obj.g_call_on_f_public_and_protected();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
  • 相关阅读:
    【Android 四大组件之Content Provider】一文吃透 BroadcastReceiver 广播接收器
    P1113 杂务
    AIGC实战——变分自编码器(Variational Autoencoder, VAE)
    【Hive】快速入门~
    学习笔记|串口通信实战|简易串口控制器|sprintf函数|STC32G单片机视频开发教程(冲哥)|第二十一集(下):串口与PC通信
    RCNN 目标检测网络学习笔记 (附代码)
    并发编程-延时队列DelayQueue
    C++11 学习之路
    springboot系列(二十二):集成easypoi实现Excel文件的导入导出(准备篇)
    浅谈事务的四个基本特性
  • 原文地址:https://blog.csdn.net/hpx12345678/article/details/125586568