zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    asp.net中页面传值的几种经典方法
    关于ASp.NEt方面的好书,不得不看啊!!!
    Qt Creator 窗体控件自适应窗口大小布局
    自己动手打造T9510E EMUIB502新功能
    OpenCV&Qt学习之四——OpenCV 实现人脸检测与相关知识整理
    Qt 中获取本机IP地址
    嵌入式Linux中GPS信息读取与处理
    OpenCV 学习资源整理
    新Outlook邮箱的客户端设置
    Qt 中显示中文
  • 原文地址:https://www.cnblogs.com/cutepig/p/1956808.html
Copyright © 2011-2022 走看看