public,protected,private访问权限研究

结论1:一个类的public,protected和private成员(成员变量和成员函数)在另一个不相关类中的访问权限分别为可以访问,不可访问,不可访问

代码段1.1:Base.h文件

#ifndef BASE_H
#define BASE_H

class CBase
{
public:
    CBase(int o_public = 0, int o_protected = 0, int o_private = 0): m_public(o_public), m_protected(o_protected), m_private(o_private)
    {}
    int Getpublic();
    int m_public;
protected:
    int Getprotected();
    int m_protected;
private:
    int Getprivate();
    int m_private;
};

#endif
View Code

代码段1.2:Other.h文件

#ifndef OTHER_H
#define OTHER_H

#include "Base.h"

class COther
{
public:
    void testpublic();
    void testprotected();
    void testprivate();
    CBase m_base;
};

#endif
View Code

代码段1.3:Other.cpp文件

#include "Other.h"

void COther::testpublic()
{
    m_base.m_public += 1;    //ok
}
void COther::testprotected()
{
    m_base.m_protected += 1;    //error
}
void COther::testprivate()
{
    m_base.m_private += 1;    //error
}
View Code

结论2:一个类的public,protected和private成员(成员变量和成员函数)在其子类中的访问权限分别为可以访问,可以访问,不可访问

代码段2.1:Derive.h文件

#ifndef DERIVE_H
#define DERIVE_H

#include "Base.h"

class CDerive: public CBase
{
public:
    void testpublicmember();
    void testprotectedmember();
    void testprivatemember();
    void testpublicfunction();
    void testprotectedfunction();
    void testprivatefunction();
};

#endif
View Code

代码段2.2:Base.cpp文件

#include "Base.h"

int CBase::Getprotected()
{
    return m_protected;
}

int CBase::Getprivate()
{
    return m_private;
}

int CBase::Getpublic()
{
    return m_public;
}
View Code

代码段2.3:Derive.cpp文件

#include "Derive.h"

void CDerive::testpublicmember()
{
    m_public += 1;    //ok
}
void CDerive::testprotectedmember()
{
    m_protected +=1;    //ok
}
void CDerive::testprivatemember()
{
    m_private += 1;        //error
}
void CDerive::testpublicfunction()
{
    int a = Getpublic();        //ok
}
void CDerive::testprotectedfunction()
{
    int b = Getprotected();        //ok
}
void CDerive::testprivatefunction()
{
    int c = Getprivate();        //error
}
View Code

结论3:public,protected和private继承影响的是父类和子类之外的其他代码访问子类中父类成员的副本时的访问权限。

结论3.1:public继承时,子类中父类副本的成员在其他代码处的访问权限与父类相同(即如果一个和子类不相关的类那么它可以访问子类中父类副本的public成员,不能访问protected和private;如果一个类继承与子类(无论如何继承),那么它可以访问子类中父类副本的public成员和protected成员,不能访问private成员)。

代码段3.1.1:main.cpp文件

#include "Base.h"
#include "Derive.h"

int main()
{
    CDerive derive;
    derive.m_public += 1;        //ok
    derive.m_protected += 1;    //error
    derive.m_private += 1;        //error
        return 0;  
}
View Code

代码段3.1.2:Grandchild.h

#ifndef GRANDCHILD_H
#define GRANDCHILD_H

#include "Derive.h"

class CGrandchild: public CDerive
{
public:
    void gtestpublic();
    void gtestprotected();
    void gtestprivate();
};

#endif
View Code

代码段3.1.3:Grandchild.cpp

#include "Grandchild.h"

void CGrandchild::gtestpublic()
{
    m_public += 1;        //ok
}
void CGrandchild::gtestprotected()
{
    m_protected += 1;        //ok    
}
void CGrandchild::gtestprivate()
{
    m_private += 1;        //error
}
View Code

结论3.2:protected继承时,子类中父类副本的成员在其他代码处的访问权限被限制在protected之内(即如果一个和子类不相关的类那么它不可以访问子类中父类副本的任何成员;如果一个类继承与子类(无论如何继承),那么它可以访问子类中父类副本的public成员和protected成员,不能访问private成员)。

代码段3.2.1:Derive.h

#ifndef DERIVE_H
#define DERIVE_H

#include "Base.h"

class CDerive: protected CBase
{
public:
    void testpublicmember();
    void testprotectedmember();
    void testprivatemember();
    void testpublicfunction();
    void testprotectedfunction();
    void testprivatefunction();
};

#endif
View Code

代码段3.2.2:main.cpp

#include "Base.h"
#include "Derive.h"

int main()
{
    CDerive derive;
    derive.m_public += 1;        //error
    derive.m_protected += 1;    //error
    derive.m_private += 1;        //error
}
View Code

代码段3.2.3:Grandchild.cpp

#include "Grandchild.h"

void CGrandchild::gtestpublic()
{
    m_public += 1;        //ok
}
void CGrandchild::gtestprotected()
{
    m_protected += 1;        //ok    
}
void CGrandchild::gtestprivate()
{
    m_private += 1;        //error
}
View Code

结论3.3:private继承时,子类中父类副本的成员在其他代码处的访问权限被限制在private之内(即无论是一个和子类不相关的类还是一个继承子类的类内都不可以访问子类中父类副本的任何成员。

代码段3.3.1:Derive.h

#ifndef DERIVE_H
#define DERIVE_H

#include "Base.h"

class CDerive: private CBase
{
public:
    void testpublicmember();
    void testprotectedmember();
    void testprivatemember();
    void testpublicfunction();
    void testprotectedfunction();
    void testprivatefunction();
};

#endif
View Code

代码段3.3.2:main.cpp

#include "Base.h"
#include "Derive.h"

int main()
{
    CDerive derive;
    derive.m_public += 1;        //error
    derive.m_protected += 1;    //error
    derive.m_private += 1;        //error
}
View Code

代码段3.3.3:Grandchild.cpp

#include "Grandchild.h"

void CGrandchild::gtestpublic()
{
    m_public += 1;        //error
}
void CGrandchild::gtestprotected()
{
    m_protected += 1;        //error    
}
void CGrandchild::gtestprivate()
{
    m_private += 1;        //error
}
View Code

 

posted @ 2015-05-26 21:48  lisiyuannnn  阅读(251)  评论(0编辑  收藏  举报