C++中public,protected,private派生类继承问题和访问权限问题

C++中public,protected,private派生类继承问题和访问权限问题

当一个子类从父类继承时,父类的所有成员成为子类的成员,此时对父类成员的访问状态由继承时使用的继承限定符决定。

 

1.如果子类从父类继承时使用的继承限定符是public,那么
(1)父类的public成员成为子类的public成员,允许类以外的代码访问这些成员;
(2)父类的private成员仍旧是父类的private成员,子类成员不可以访问这些成员;
(3)父类的protected成员成为子类的protected成员,只允许子类成员访问;

 

2.如果子类从父类继承时使用的继承限定符是protected,那么

(1)父类的public成员成为子类的protected成员,只允许子类成员访问;
(2)父类的private成员仍旧是父类的private成员,子类成员不可以访问这些成员;

(3)父类的public成员成为子类的protected成员,只允许子类成员访问

 

 

3.如果子类从父类继承时使用的继承限定符是private,那么

(1)父类的public成员成为子类的private成员,只允许子类成员访问;
(2)父类的private成员仍旧是父类的private成员,子类成员不可以访问这些成员;
(3)父类的protected成员成为子类的private成员,只允许子类成员访问;

 

其实这些都很有的规律的,子类public时表示最大的继承权限是public,所以子类按照原样继承,子类protected继承时最大继承权限是protected, 所以基类的public成员降级成为protected了....子类private继承时所以都成为private了, 不过子类不能访问基类的private成员..

 

子类默认的是private继承基类...

 

举个使用private继承的例子,Boost::Utility库的不可以复制的类 noncopyable

 

#include "boost/utility.hpp"

或者是

#include "boost/noncopyable.hpp"

 

[cpp] view plain copy
 
 print?
  1. #ifndef BOOST_NONCOPYABLE_HPP_INCLUDED  
  2. #define BOOST_NONCOPYABLE_HPP_INCLUDED  
  3.   
  4. namespace boost {  
  5.   
  6. //  Private copy constructor and copy assignment ensure classes derived from  
  7. //  class noncopyable cannot be copied.  
  8.   
  9. //  Contributed by Dave Abrahams  
  10.   
  11. namespace noncopyable_  // protection from unintended ADL  
  12. {  
  13.   class noncopyable  
  14.   {  
  15.    protected:  
  16.       noncopyable() {}  
  17.       ~noncopyable() {}  
  18.    private:  // emphasize the following members are private  
  19.       noncopyable( const noncopyable& );  
  20.       const noncopyable& operator=( const noncopyable& );  
  21.   };  
  22. }  
  23.   
  24. typedef noncopyable_::noncopyable noncopyable;  
  25.   
  26. // namespace boost  
  27.   
  28. #endif  // BOOST_NONCOPYABLE_HPP_INCLUDED  



 

类 boost::noncopyable 被规定为作为私有基类来使用,它可以有效地关闭复制构造和赋值操作:

 

 

[cpp] view plain copy
 
 print?
  1. #include "boost/utility.hpp"  
  2.   
  3. class please_dont_make_copies : boost::noncopyable {  
  4. public:  
  5.   void do_stuff() {  
  6.     std::cout << "Dear client, you just cannot copy me!";  
  7.  }  
  8. };  

 

 

这样就禁止了复制和赋值....

 

 

=========================================================================

c++ big three

 

 

三法则英语rule of threethe Law of The Big ThreeThe Big Three三法则三大定律)在 C++ 程序设计里,它是一个以设计的基本原则而制定的定律,三法则的要求在于,假如类型有明显地定义下列其中一个成员函数,那么程序员必须连其他二个成员函数也一同编写至类型内,亦即下列三个成员函数缺一不可。 [1]:

上述三个函数是特别的成员函数,假如程序员没有自行定义或是编写声明它们,那么编译器会自动地创建它们,并且会编译至应用程序内。相反地,假如程序员有定义上述三者其中一个函数,那么由编译器自动产生出来的上述三个函数是不会搭配到这个类型内。三法则(Rule of three)这个专有名词是由 Marshall Cline 于 1991 年创立的[2]

 

class_a.h文件

 

[cpp] view plain copy
 
 print?
  1. #ifndef _CLASS_A_H_  
  2. #define _CLASS_A_H_  
  3.    
  4. #ifndef _MSC_VER  
  5. #undef NULL  
  6. #define NULL 0  
  7. #endif  
  8. #include <iostream>  
  9. #include <cstdlib>  
  10. #define BUFFER_SIZE 7  
  11.    
  12. using namespace std;  
  13.    
  14. class ClassA  
  15. {  
  16. public:  
  17.     // 三種建構子  
  18.     ClassA()  
  19.     {  
  20.         cout<<"ClassA():"<<endl;  
  21.         this->setAlloc(BUFFER_SIZE);  
  22.         this->setData();  
  23.     }  
  24.     ClassA(const int n)  
  25.     {  
  26.         cout<<"ClassA(const int n):"<<endl;  
  27.         this->setAlloc(n);  
  28.         this->setData();  
  29.     }  
  30.     // 複製建構子  
  31.     ClassA(const ClassA& clone)  
  32.     {  
  33.         cout<<"ClassA(const ClassA& clone):"<<endl;  
  34.         this->setAlloc(clone.m_N);  
  35.         this->setData(clone.m_pn);  
  36.     }  
  37.     // 複製指定運算子成員函式  
  38.     ClassA& operator=(const ClassA& clone)  
  39.     {  
  40.         cout<<"ClassA& operator=(const ClassA& clone)"<<endl;  
  41.         // 保護:禁止自己設值給自己  
  42.         if ( this != &clone )  
  43.         {  
  44.             this->setData(clone.m_pn);  
  45.         }  
  46.         return *this;  
  47.     }  
  48.     // 解構子  
  49.     ~ClassA()  
  50.     {  
  51.         cout<<"~Destructor!!!"<<endl;  
  52.         // 釋放記憶體  
  53.         delete [] this->m_pn;  
  54.     }  
  55.     // 配置  
  56.     void setAlloc(const int n)  
  57.     {  
  58.         this->m_N = n;  
  59.         // 配置一塊記憶體給指標  
  60.         this->m_pn = new int[this->m_N];  
  61.     }  
  62.     // 填入一堆的整數值  
  63.     void setData(int* pn = NULL)  
  64.     {  
  65.         for ( int i = 0; i < this->m_N; i ++)  
  66.         {  
  67.             // 給初始值  
  68.             if ( pn == NULL )  
  69.             {  
  70.                 this->m_pn[i] = (2 * i + 1);  
  71.             }  
  72.             // 複製指標儲存的整數值  
  73.             else  
  74.             {  
  75.                 this->m_pn[i] = pn[i];  
  76.             }  
  77.         }  
  78.     }  
  79.     // 列印顯示  
  80.     void print(void)  
  81.     {  
  82.         for ( int i = 0; i < this->m_N; i ++)  
  83.         {  
  84.             cout<<" "<<this->m_pn[i];  
  85.         }  
  86.         cout<<endl;  
  87.     }  
  88. private:  
  89.     // 指標  
  90.     int* m_pn;  
  91.     // 元素個數  
  92.     int m_N;  
  93. };  
  94.    
  95. #endif  



 

主函数

 

[cpp] view plain copy
 
 print?
  1. // Headers and Macros  
  2. #ifndef _MSC_VER  
  3. #undef NULL  
  4. #define NULL 0  
  5. #endif  
  6. #include <iostream>  
  7. #include <cstdlib>  
  8. #include "class_a.h"  
  9. using namespace std;  
  10. //  
  11. //Main Function  
  12. #ifndef _MSC_VER  
  13. int  
  14. #else  
  15. void  
  16. #endif  
  17. main(int argc, char** argv)  
  18. {  
  19.     // 區塊  
  20.     {  
  21.         // 建立第一個物件  
  22.         ClassA A(BUFFER_SIZE);  
  23.         cout<<" A =>";  
  24.         A.print();  
  25.         {  
  26.             // 開始執行 ClassA(const ClassA& clone)  
  27.             ClassA B = A;  
  28.             cout<<" B =>";  
  29.             B.print();  
  30.         }  
  31.         {  
  32.             ClassA C;  
  33.             // 開始執行 ClassA& operator=(const ClassA& clone)  
  34.             C = A;  
  35.             cout<<" C =>";  
  36.             C.print();  
  37.         }  
  38.     }  
  39.     system("PAUSE");  
  40.     return  
  41. #ifndef _MSC_VER  
  42.         EXIT_SUCCESS  
  43. #endif  
  44.         ;  
  45. }  



 

http://zh.wikipedia.org/wiki/%E4%B8%89%E6%B3%95%E5%89%87_(C%2B%2B%E7%A8%8B%E5%BC%8F%E8%A8%AD%E8%A8%88)

 

posted @ 2016-04-10 11:12  菜鸡一枚  阅读(2728)  评论(0编辑  收藏  举报