找回密码
 立即注册
  • QQ空间
  • 回复
  • 收藏

C++类继承与九种情况下权限实验

admin 2019-8-11 15:12 94人围观 C++相关



      #include<iostream>usingnamespace std;/*访问权限继承权限 类内 类外1private public N N2protected public Y N3public public Y Y
      4private protected N N5protected protected Y N 6public protected Y N
      7private private N N8protected private Y N 9public private Y N

      */




      /*第一种情况:访问权限为private,继承权限为public
      classTime //基类(父类){private:inth;protected:intm;public:ints;};classDate:public Time //派生类(子类){public:intfun(){h = 25; //类内不可访问基类中的privatereturn0;}};intmain(){DateA;A.h = 20;return0; //类外不可访问基类中的private}*/
      /*
      第二种情况:访问权限为protected,继承权限为public
      classTime{//基类(父类)private:inth;protected:intm;public:ints;};classDate :public Time{//派生类(子类)public:intfun(){m = 12; //类内允许访问基类中protectedreturn0;}};intmain(){DateA;A.m = 58; //l类外不允许访问基类中的protectedreturn0;}*/
      /*
      第三种情况:访问权限为public,继承权限为public
      classTime{//基类(父类)private:inth;protected:intm;public:ints;};classDate :public Time{//派生类(子类)public:intfun(){s= 12; //类内允许访问基类中的publicreturn0;}};intmain(){DateA;A.s = 26; //类外允许访问基类中的publicreturn0;}*/

      /*
      第四种情况:访问权限为private,继承权限为protected
      classTime{//基类(父类)private:inth;protected:intm;public:ints;};classDate :protected Time{//派生类(子类)protected:intfun(){h = 12; //类内不允许访问基类中的privatereturn0;}};intmain(){DateA;A.h = 36; //类外不允许访问基类中的privatereturn0;}*/
      /*
      第五种情况:访问权限为protected,继承权限为protected
      classTime{//基类(父类)private:inth;protected:intm;public:ints;};classDate :protected Time{//派生类(子类)protected:intfun(){m = 12; //类内允许访问基类中的protectedreturn0;}};intmain(){DateA;A.m = 36; //类外不允许访问基类中的protectedreturn0;}*/
      /*
      第六种情况:访问权限为public,继承权限为protected
      classTime{//基类(父类)private:inth;protected:intm;public:ints;};classDate :protected Time{//派生类(子类)protected:intfun(){s = 12; //类内允许访问基类中的publicreturn0;}};intmain(){DateA;A.s = 36; //类外不允许访问基类中的publicreturn0;}*/
      /*
      第七种情况:访问权限为private,继承权限为private
      classTime{//基类(父类)private:inth;protected:intm;public:ints;};classDate :private Time{//派生类(子类)private:intfun(){h = 36; //类内不允许访问privatereturn0;}};intmain(){DateA;A.h = 36; //类外不允许访问privatereturn0;}*/
      /*
      第八种情况:访问权限为protected,继承权限为private
      classTime{//基类(父类)private:inth;protected:intm;public:ints;};classDate :private Time{//派生类(子类)private:intfun(){m = 36; //类内允许访问protected;return0;}};intmain(){DateA;A.m = 25; //类外不允许访问protectedreturn0;}*/

      classTime{private:inth;protected:intm;public:ints;};classDate :private Time{private:intfun(){s = 25; //类内允许访问publicreturn0;}};intmain(){DateA;A.s = 26; //类外不允许访问publicreturn0;}

      ----------------------------------------------------------------------------------------------------------------------
      我们尊重原创,也注重分享,文章来源于微信公众号:SUPER CODING,建议关注公众号查看原文。如若侵权请联系qter@qter.org。
      ----------------------------------------------------------------------------------------------------------------------

      鲜花

      握手

      雷人

      路过

      鸡蛋

      yafeilinux和他的朋友们微信公众号二维码

      微信公众号

      专注于Qt嵌入式Linux开发等。扫一扫立即关注。

      Qt开源社区官方QQ群二维码

      QQ交流群

      欢迎加入QQ群大家庭,一起讨论学习!

      我有话说......