• C++: public/protected/private inheritance


    class A
    {
        public:
            int a;
        private:
            int b;
        protected:
            int c;
    };

    //
    // public inheritance:
    //        - data access type not change
    //        - B cannot access  A's private member
    //
    class B: public A
    {
        public:
        void test()
        {
            a = 1;
            //b = 1;    //Fail
            c = 1;
        }
    };

    //
    // protected inheritance:
    //        - data access type: public -> protected
    //        - C cannot access  A's private member
    //
    class C: protected A
    {
        public:
        void test()
        {
            a = 1;
            //b = 1;    //Fail
            c = 1;
        }
    };

    //
    // private inheritance:
    //        - data access type: public , protected -> private
    //        - D cannot access  A's private member
    //
    class D: private A
    {
    public:
        void test()
        {
            a = 1;
            //b = 1;    //Fail
            c = 1;
        }
    };

    class E: public C
    {
        public:
        void test()
        {
            a = 1;
            //b = 1;
            c = 1;
        }
    };

    class F: public D
    {
        void test()
        {
            //a = 1;
            //b = 1;
            //c = 1;
        }
    };

    int main()
    {
        {
            B b;
            b.test();
            b.a = 1;
            //b.b = 1;
            //b.c = 1;
        }

        {
            C b;
            b.test();
            //b.a = 1;
            //b.b = 1;
            //b.c = 1;
        }

        {
            D b;
            b.test();
            //b.a = 1;
            //b.b = 1;
            //b.c = 1;
        }

        {
            E b;
            b.test();
            //b.a = 1;
            //b.b = 1;
            //b.c = 1;
        }
    }
  • 相关阅读:
    css小技巧: select的css控制
    js中不存在exit函数,程序的运行中断停止,可使用return
    转载: WebCore渲染之一:基础
    转载: WEB架构师成长系列索引
    js:<form></form>中有<a>按钮时不能跳转?
    小心得:前台与后台的数据传递
    php session和cookie使用说明
    css 字体使用
    转载: PHP引用(&)使用详解
    三层架构下的增删改查 荣
  • 原文地址:https://www.cnblogs.com/cutepig/p/1956808.html
Copyright © 2020-2023  润新知