Skip to content

Latest commit

 

History

History
89 lines (73 loc) · 1.5 KB

File metadata and controls

89 lines (73 loc) · 1.5 KB
class Base {
public:
  void pub_mem(); 
protected:
  int prot_mem; 
private:
  char priv_mem; 
};

struct Pub_Derv : public Base {
  int f() { return prot_mem; }
};

struct Priv_Derv : private Base {
  int f() { return prot_mem; }
};

struct Prot_Derv : protected Base {
  int f() { return prot_mem; }
};

struct Derived_from_Public : public Pub_Derv {
  int use_base() { return prot_mem; }
};

struct Derived_from_Private : public Priv_Derv {
  int use_base() { return prot_mem; }
};

struct Derived_from_Protected : public Prot_Derv {
  int use_base() { return prot_mem; }
};

int main() {
  Pub_Derv d1; Priv_Derv d2; Prot_Derv d3;
  Derived_from_Public dd1; Derived_from_Private dd2; Derived_from_Protected dd3;
  Base *p = &d1;  
  p = &d2;                
  p = &d3;        
  p = &dd1;       
  p = &dd2;       
  p = &dd3;      

  return 0;
}
class Base {
public:
  void pub_mem(); 
protected:
  int prot_mem; 
private:
  char priv_mem; 
};

struct Pub_Derv : public Base {
  //合法
  void memfcn(Base &b) { b = *this; }
};

struct Priv_Derv : private Base {
  //合法
  void memfcn(Base &b) { b = *this; }
};

struct Prot_Derv : protected Base {
  //合法
  void memfcn(Base &b) { b = *this; }
};

struct Derived_from_Public : public Pub_Derv {
  //合法
  void memfcn(Base &b) { b = *this; }
};

struct Derived_from_Private : public Priv_Derv {
  //非法
  void memfcn(Base &b) { b = *this; }
};

struct Derived_from_Protected : public Prot_Derv {
  //合法
  void memfcn(Base &b) { b = *this; }
};