• 【C++基础】公有继承,保护继承,私有继承


    公有继承(public)

    公有继承,对父类的公有成员和保护成员的访问属性不变,子类(派生类)的新增成员可以访问基类的公有成员和保护成员,但是访问不了父类的私有成员。子类(派生类)的对象只能访问子类(派生类)的公有成员(包括继承的公有成员),访问不了保护成员和私有成员。
    在这里插入图片描述

    //父类
    class Base
    {
        public:
            Base(int nId) {
                mId = nId;
            }
            int GetId() {
                cout<<"Base中的GetId()"<<endl;
                mId++;
                cout<< mId<<endl;
                return mId;
            }
        protected:
            int GetNum() {
                cout<< 0 <<endl;
                return 0;
            }
        private:
            int mId;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    //公有继承 public
    class Child : public Base
    {
        public:
            Child() : Base(7){
                ;
            }
            //新增成员可以访问公有成员
            int GetCId() {
                cout<<"Child中的GetCId(),调用Base中public的GetId()"<<endl;
                return GetId();
            }
            //新增成员可以访问保护成员
            int GetCNum() {
                cout<<"Child中的GetCId(),调用Base中protected的GetNum()"<<endl;
                return GetNum();
            }
            //无法访问基类的私有成员
        protected:
            int y;
        private:
            int x;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    int main()
    {
        Child child;
        child.GetId();        //派生类的对象可以访问派生类继承下来的公有成员
        //child.GetNum();     //无法访问继承下来的保护成员GetNum()
        child.GetCId();
        child.GetCNum();      //派生类对象可以访问派生类的公有成员
        //child.y;            //无法访问派生类的保护成员y和私有成员x
        //child.x;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    保护继承(protected

    保护继承,父类的公有成员和保护成员被子类(派生类)继承后变成保护成员,子类(派生类)的新增成员可以访问父类的公有成员和保护成员,但是访问不了父类的私有成员。子类(派生类)的对象不能访问子类(派生类)继承父类的公有成员,保护成员和私有成员。
    在这里插入图片描述

    class Base
    {
        public:
            Base(int nId) {
                mId = nId;
            }
            int GetId() {
                cout<<"Base中的GetId()"<<endl;
                mId++;
                cout<< mId<<endl;
                return mId;
            }
        protected:
            int GetNum() {
                cout<< 0 <<endl;
                return 0;
            }
        private:
            int mId;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    //保护继承(protected)
    class Child : protected Base
    {
        public:
            Child() : Base(7) {
                ;
            }
            //可以访问父类的公有成员和保护成员
            int GetCId() {
                cout<<"Child中的GetCId(),调用Base中public的GetId()"<<endl;
                return GetId();
            }
            //可以访问父类的公有成员和保护成员
            int GetCNum() {
                cout<<"Child中的GetCId(),调用Base中protected的GetNum()"<<endl;
                return GetNum();
            }
        protected:
            int y;
        private:
            int x;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    int main()
    {
        Child child;
        //child.GetId();//子类(派生类)对象访问不了继承的公有成员,因为此时保护继承时GetId()已经为:protected类型
        //child.GetNum(); //这个也访问不了
        child.GetCId();
        child.GetCNum();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    私有继承(private)

    私有继承时,父类的公有成员和保护成员都被子类(派生类)继承下来之后变成私有成员,子类(派生类)的新增成员可以访问父类的公有成员和保护成员,但是访问不了父类的私有成员。子类(派生类)的对象不能访问子类(派生类)继承父类的公有成员,保护成员和私有成员。
    在这里插入图片描述

    class Base
    {
        public:
            Base(int nId) {
                mId = nId;
            }
            int GetId() {
                cout<<"Base中的GetId()"<<endl;
                mId++;
                cout<< mId<<endl;
                return mId;
            }
        protected:
            int GetNum() {
                cout<< 0 <<endl;
                return 0;
            }
        private:
            int mId;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    //私有继承(private)
    class Child : private Base
    {
        public:
            Child() : Base(7) {
                ;
            }
            //可以访问基类的公有成员和保护成员
            int GetCId() {
                cout<<"Child中的GetCId(),调用Base中public的GetId()"<<endl;
                return GetId();
            }
            //可以访问基类的公有成员和保护成员
            int GetCNum() {
                cout<<"Child中的GetCId(),调用Base中protected的GetNum()"<<endl;
                return GetNum();
            }
        protected:
            int y;
        private:
            int x;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    int main()
    {
        Child child;
        //child.GetId();//子类(派生类)对象访问不了继承的公有成员,因为此时私有继承时GetId()已经为:private类型
        //child.GetNum(); //子类(派生类)对象访问不了继承的保护成员,而且此时私有继承时GetNum()已经为:private类型
        child.GetCId();
        child.GetCNum();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    公有继承,保护继承,私有继承小结

    无论是哪种继承方式,子类(派生类)中新增成员可以访问父类的公有成员和保护成员,无法访问私有成员。
    而继承方式影响的是子类(派生类)继承成员访问属性,而使用友元(friend)可以访问保护成员和私有成员。

    父类访问属性public成员protected成员private成员
    public继承××
    protected继承×××
    private继承×××
  • 相关阅读:
    django建站过程(1)
    使用Clion软件实现基于国密SM2-SM3的SSL安全通信
    云服务器登录方式
    STM32合并烧录IAP+APP
    流媒体直播播放协议:HLS、RTMP、HTTP-FLV
    Flink处理函数(一)
    【MySQL】MySQL基础、详细总结
    白盒测试之测试用例设计方法
    【ES6知识】ESModule 模块化
    【计算机组成原理】数据的存储和排列
  • 原文地址:https://blog.csdn.net/qq_44033208/article/details/127555593