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

2023-05-09,,

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"

    #ifndef BOOST_NONCOPYABLE_HPP_INCLUDED
    #define BOOST_NONCOPYABLE_HPP_INCLUDED
    namespace boost {
    //  Private copy constructor and copy assignment ensure classes derived from
    //  class noncopyable cannot be copied.
    //  Contributed by Dave Abrahams
    namespace noncopyable_  // protection from unintended ADL
    {
    class noncopyable
    {
    protected:
    noncopyable() {}
    ~noncopyable() {}
    private:  // emphasize the following members are private
    noncopyable( const noncopyable& );
    const noncopyable& operator=( const noncopyable& );
    };
    }
    typedef noncopyable_::noncopyable noncopyable;
    } // namespace boost
    #endif  // BOOST_NONCOPYABLE_HPP_INCLUDED

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

    #include "boost/utility.hpp"
    class please_dont_make_copies : boost::noncopyable {
    public:
    void do_stuff() {
    std::cout << "Dear client, you just cannot copy me!";
    }
    };

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

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

c++ big three

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

析构函数(Destructor)
复制构造函数(copy constructor)
复制赋值运算符(copy assignment operator)

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

class_a.h文件

    #ifndef _CLASS_A_H_
    #define _CLASS_A_H_
    #ifndef _MSC_VER
    #undef NULL
    #define NULL 0
    #endif
    #include <iostream>
    #include <cstdlib>
    #define BUFFER_SIZE 7
    using namespace std;
    class ClassA
    {
    public:
    // 三種建構子
    ClassA()
    {
    cout<<"ClassA():"<<endl;
    this->setAlloc(BUFFER_SIZE);
    this->setData();
    }
    ClassA(const int n)
    {
    cout<<"ClassA(const int n):"<<endl;
    this->setAlloc(n);
    this->setData();
    }
    // 複製建構子
    ClassA(const ClassA& clone)
    {
    cout<<"ClassA(const ClassA& clone):"<<endl;
    this->setAlloc(clone.m_N);
    this->setData(clone.m_pn);
    }
    // 複製指定運算子成員函式
    ClassA& operator=(const ClassA& clone)
    {
    cout<<"ClassA& operator=(const ClassA& clone)"<<endl;
    // 保護:禁止自己設值給自己
    if ( this != &clone )
    {
    this->setData(clone.m_pn);
    }
    return *this;
    }
    // 解構子
    ~ClassA()
    {
    cout<<"~Destructor!!!"<<endl;
    // 釋放記憶體
    delete [] this->m_pn;
    }
    // 配置
    void setAlloc(const int n)
    {
    this->m_N = n;
    // 配置一塊記憶體給指標
    this->m_pn = new int[this->m_N];
    }
    // 填入一堆的整數值
    void setData(int* pn = NULL)
    {
    for ( int i = 0; i < this->m_N; i ++)
    {
    // 給初始值
    if ( pn == NULL )
    {
    this->m_pn[i] = (2 * i + 1);
    }
    // 複製指標儲存的整數值
    else
    {
    this->m_pn[i] = pn[i];
    }
    }
    }
    // 列印顯示
    void print(void)
    {
    for ( int i = 0; i < this->m_N; i ++)
    {
    cout<<" "<<this->m_pn[i];
    }
    cout<<endl;
    }
    private:
    // 指標
    int* m_pn;
    // 元素個數
    int m_N;
    };
    #endif

主函数

    // Headers and Macros
    #ifndef _MSC_VER
    #undef NULL
    #define NULL 0
    #endif
    #include <iostream>
    #include <cstdlib>
    #include "class_a.h"
    using namespace std;
    //
    //Main Function
    #ifndef _MSC_VER
    int
    #else
    void
    #endif
    main(int argc, char** argv)
    {
    // 區塊
    {
    // 建立第一個物件
    ClassA A(BUFFER_SIZE);
    cout<<" A =>";
    A.print();
    {
    // 開始執行 ClassA(const ClassA& clone)
    ClassA B = A;
    cout<<" B =>";
    B.print();
    }
    {
    ClassA C;
    // 開始執行 ClassA& operator=(const ClassA& clone)
    C = A;
    cout<<" C =>";
    C.print();
    }
    }
    system("PAUSE");
    return
    #ifndef _MSC_VER
    EXIT_SUCCESS
    #endif
    ;
    }

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)

C++中public,protected,private派生类继承问题和访问权限问题的相关教程结束。

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

下载本文的Word格式文档,以方便收藏与打印。