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;
        }
    }
  • 相关阅读:
    人工智能数学基础笔记(上)
    人工智能简介
    十三,十四 基金收益,税收与基金国际化
    资产配置模型之-BL模型
    十二 基金估值,费用与会计核算
    十一 基金的投资交易与结算
    十 基金业绩评价
    九 投资风险管理
    浙工商oj ___飞龙的飞行方程
    hd1004解题思路
  • 原文地址:https://www.cnblogs.com/cutepig/p/1956808.html
Copyright © 2011-2022 走看看