- #include <iostream>
-
#include <cstring>
-
-
class Cparent
-
{
-
public:
-
int x;
-
protected:
-
int y;
-
private:
-
int z;
-
};
-
-
class Cchild:public Cparent
-
{
-
public:
-
void visitx(int xx);
-
void visity(int yy);
-
void visitz(int zz);
-
};
-
-
void Cchild::visitx(int xx)
-
{
-
x=xx;
-
}
-
-
void Cchild::visity(int yy)
-
{
-
y=yy;
-
}
-
-
void Cchild::visitz(int zz)
-
{
-
z=zz;
-
}
-
-
int main()
-
{
-
return 0;
- }
1. 当访问权限是 public时,基类中的 public = public
protected = protected
private = private
其中,private的数据成员 不能被 继承类、类的对象访问
-
private:
- int z;
-
void Cchild::visitz(int zz)
-
{
-
z=zz;
- }
当我们 编译 这个程序时,程序报错,说明 private 不能被
继承类访问
- ywx@yuweixian:~/yu/c++/pais$ g++ -o pais pais.cpp
-
pais.cpp: In member function ‘void Cchild::visitz(int)’:
-
pais.cpp:11: error: ‘int Cparent::z’ is private
-
pais.cpp:34: error: within this context
- ywx@yuweixian:~/yu/c++/pais$
2. 当为,protected :本类、继承类 可以访问,类的对象
不能访问