当前位置:   article > 正文

类的继承一:派生类对基类成员的访问权限_派生类继承基类所以成员

派生类继承基类所以成员

总的规则就是:派生类的访问权限规则如下:
1.不管以什么继承方式,派生类内部都不能访问基类的私有成员。
2.不管以什么继承方式,派生类内部除了基类的私有成员不可以访问外,其他的都可以访问。
3.不管以什么继承方式,派生类对象除了公有继承基类中的公有成员可以访问外,其他的一律不能访问
我们先试一下public继承

#include <iostream>

using std::cout;
using std::endl;

class Point
{
public:
    Point(int ix=0,int iy=0)
        :_ix(ix)
         ,_iy(iy)
    {
        cout<<"Point(int = 0,int = 0)"<<endl;
    }
    int getY()const
    {
        return _iy;
    }
    ~Point()
    {
        cout<<"~Point()"<<endl;
    }
protected:
    int _ix;
private:
    int _iy;
};

class Point3D
:public Point
{
public:
    Point3D(int ix = 0,int iy = 0,int iz = 0)
        :Point(ix,iy)
        , _iz(iz)
    {
        cout<<"Point3D(int=0,int=0,int=0)"<<endl;
    }
    void print()
    {
        cout<<"_ix="<<_ix<<endl//protected
            <<"_iy="<<getY()<<endl//public
            /* <<"_iy="<<_iy<<endl;//error,基类的私有成员不能在类外进行访问 */
            <<"_iz="<<_iz<<endl;
    }
    ~Point3D()
    {
        cout<<"~Point3D()"<<endl;
    }
private:
    int _iz;
};
int main()
{
    Point3D pt3d(1,2,3);
    pt3d.getY();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

可以完美的编译通过

接下来private继承又有点不同

#include <iostream>

using std::cout;
using std::endl;

class Point
{
public:
    Point(int ix=0,int iy=0)
        :_ix(ix)
        ,_iy(iy)
    {
        cout<<"Point(int=0,int=0)"<<endl;
    }
    int getY()const
    {
        return _iy;
    }
    ~Point()
    {
        cout<<"~Point()"<<endl;
    }
protected:
    int _ix;
private:
    int _iy;
};

class Point3D
:private Point
{
public:
    Point3D(int ix=0,int iy=0,int iz=0)
        :Point(ix,iy)
         ,_iz(iz)
    {
        cout<<"Point3D(int=0,int=0,int=0)"<<endl;
    }
    void print()
    {
        cout<<"_ix="<<_ix<<endl//private
            <<"_iy="<<getY()<<endl//private
            /* <<"_iy="<<_iy<<endl//error,基类的私有成员不能在类外进行访问 */
            <<"_iz="<<_iz<<endl;
    }
    ~Point3D()
    {
        cout<<"~Point3D()"<<endl;
    }
private:
    int _iz;
};
class Point4D
:private Point3D
{
public:
    void show()
    {
        /* cout<<"_ix="<<_ix<<endl */
        /*     <<"_iy="<<getY()<<endl; */
        cout<<"_im="<<_im<<endl;
    }
private:
    int _im;
};
int main()
{
    Point3D pt3d(1,2,3);
    /* pt3d._ix=100;//error */
    /* pt3d.getY();//error */
    return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73

对一个私有继承再继承一次的话,Point3D所有的所有成员都是private,Point4D的便不能访问Point3D的任何成员。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/106134
推荐阅读
相关标签
  

闽ICP备14008679号