Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4242225
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-04-20 10:06:02



  1. #include <iostream>
  2. #include <cstring>

  3. class Cparent
  4. {
  5. public:
  6.    int x;
  7. protected:
  8.    int y;
  9. private:
  10.    int z;
  11. };

  12. class Cchild:public Cparent
  13. {
  14. public:
  15.     void visitx(int xx);
  16.     void visity(int yy);
  17.     void visitz(int zz);
  18. };

  19. void Cchild::visitx(int xx)
  20. {
  21.     x=xx;
  22. }

  23. void Cchild::visity(int yy)
  24. {
  25.     y=yy;
  26. }

  27. void Cchild::visitz(int zz)
  28. {
  29.     z=zz;
  30. }

  31. int main()
  32. {
  33.     return 0;
  34. }

1. 当访问权限是 public时,基类中的 public    = public
                                protected = protected
                                private   = private

其中,private的数据成员 不能被 继承类、类的对象访问

  1. private:
  2.    int z;

  1. void Cchild::visitz(int zz)
  2. {
  3.     z=zz;
  4. }

当我们 编译 这个程序时,程序报错,说明 private 不能被

继承类访问


  1. ywx@yuweixian:~/yu/c++/pais$ g++ -o pais pais.cpp
  2. pais.cpp: In member function ‘void Cchild::visitz(int):
  3. pais.cpp:11: error: ‘int Cparent::z’ is private
  4. pais.cpp:34: error: within this context
  5. ywx@yuweixian:~/yu/c++/pais$


2.  当为,protected :本类、继承类 可以访问,类的对象

不能访问











阅读(988) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~