c++ 继承(杂)

来源:互联网 发布:西安汇知中学教师招聘 编辑:程序博客网 时间:2024/05/17 04:02
public是这个程序的每一处都能访问,无论是在本类内还本类外
protected则是在本类内和友类,子类中才能访问(只能在类的成员函数中调用 )
private则只能在本类内和友类才能访问,其它地方则不能(和protected 区别 ;不能在子类中访问)


class base
{
   public:
        int a;
        void f(){
            b;//true
           c;//true
        };
       friend void pFun(const base&a);//声明友元函数 必须在外部定义   
   protected:
          int b;
   private:
           int c;
}
void pFun(const base&a)
{
   cout<<c;//true;
   cout<<b;//true;
}

class Derive:public base
{
  public:
      void g(){
            this->b;//ture(调用本类实例的 b)
            __super::b;//true(调用基类的 私有数据)(或 this->base::b)
     }
     void f() {     
    }
  private:
     int c;
     int b;
}
void main
{
base a;
a.a=2;//true
a.b=2;//error 只能在类的函数中调用
a.c=2;//error 同上

        pFun(a);//调用友元函数

}

原创粉丝点击