超碰免费人人操|国产视频二区久久艹人人操|欧美激情第一页在线|久热最新无码中文视频|91精品国际成人|亚洲成人精品在线视频青青草|久草免费高清完整在线观看|你懂的AV在线日本黄网页|国产黄色AV日韩女同网|欧美成人色区导航片av

C++ 的三種訪問(wèn)權(quán)限與三種繼承方式

時(shí)間:2026-01-01 18:01:30 C語(yǔ)言

C++ 的三種訪問(wèn)權(quán)限與三種繼承方式

  我們知道C++中的類(lèi),有三種訪問(wèn)權(quán)限(也稱(chēng)作訪問(wèn)控制),它們分別是public、protected、private。要理解它們其實(shí)也很容易,以下是為大家分享的C++ 的三種訪問(wèn)權(quán)限與三種繼承方式,供大家參考借鑒,歡迎瀏覽!

  父類(lèi):

  class Person

  {

  public:

  Person(const string& name, int age) : m_name(name), m_age(age)

  {

  }

  void ShowInfo()

  {

  cout << "姓名:" << m_name << endl;

  cout << "年齡:" << m_age << endl;

  }

  protected:

  string m_name; /pic/p>

  private:

  int m_age; /pic/p>

  };

  class Person

  {

  public:

  Person(const string& name, int age) : m_name(name), m_age(age)

  {

  }

  void ShowInfo()

  {

  cout << "姓名:" << m_name << endl;

  cout << "年齡:" << m_age << endl;

  }

  protected:

  string m_name; /pic/p>

  private:

  int m_age; /pic/p>

  };

  子類(lèi):

  class Teacher : public Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "姓名:" << m_name << endl; /pic/p>

  cout << "年齡:" << m_age << endl; /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  class Teacher : public Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "姓名:" << m_name << endl; /pic/p>

  cout << "年齡:" << m_age << endl; /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  調(diào)用方法:

  void test()

  {

  Person person("張三", 22);

  person.ShowInfo(); /pic/p>

  cout << person.m_name << endl; /pic/p>

  cout << person.m_age << endl; /pic/p>

  }

  void test()

  {

  Person person("張三", 22);

  person.ShowInfo(); /pic/p>

  cout << person.m_name << endl; /pic/p>

  cout << person.m_age << endl; /pic/p>

  }

  總結(jié)

  我們對(duì)C++類(lèi)三種方式控制權(quán)限總結(jié)如下,這與Java中的三種對(duì)應(yīng)的訪問(wèn)權(quán)限是一樣的。

  qq%e6%88%aa%e5%9b%be20161104113813

  三種繼承方式

  C++中繼承的方式還有多種,也分別都用public、protected、private表示。這與Java不一樣,Java只有繼承的概念,默認(rèn)是public繼承的。

  1. 三種繼承方式不影響子類(lèi)對(duì)父類(lèi)的訪問(wèn)權(quán)限,子類(lèi)對(duì)父類(lèi)只看父類(lèi)的訪問(wèn)控制權(quán)。

  如下面三種繼承方式都能訪問(wèn)父類(lèi)中的public和protected成員。

  class Teacher : /*public*/ /*protected*/ private Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "姓名:" << m_name << endl; /pic/p>

  /pic/pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  class Teacher : /*public*/ /*protected*/ private Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "姓名:" << m_name << endl; /pic/p>

  /pic/pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  2. 繼承方式是為了控制子類(lèi)(也稱(chēng)派生類(lèi))的調(diào)用方(也叫用戶(hù))對(duì)父類(lèi)(也稱(chēng)基類(lèi))的訪問(wèn)權(quán)限。

  public繼承

  class Teacher : public Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  class Teacher : public Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  void TestPublic()

  {

  Teacher teacher("李四", 35, "副教授");

  teacher.ShowInfo();

  cout << endl;

  teacher.ShowTeacherInfo();

  }

  void TestPublic()

  {

  Teacher teacher("李四", 35, "副教授");

  teacher.ShowInfo();

  cout << endl;

  teacher.ShowTeacherInfo();

  }

  結(jié)果:

  姓名:李四

  年齡:35

  姓名:李四

  年齡:35

  職稱(chēng):副教授

  private繼承:

  class Teacher : private Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  class Teacher : private Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  void TestPrivate()

  {

  Teacher teacher("李四", 35, "副教授");

  teacher.ShowInfo(); /pic/p>

  cout << endl;

  teacher.ShowTeacherInfo();

  }

  void TestPrivate()

  {

  Teacher teacher("李四", 35, "副教授");

  teacher.ShowInfo(); /pic/p>

  cout << endl;

  teacher.ShowTeacherInfo();

  }

  3. public、protected、private三種繼承方式,相當(dāng)于把父類(lèi)的public訪問(wèn)權(quán)限在子類(lèi)中變成了對(duì)應(yīng)的權(quán)限。

  如protected繼承,把父類(lèi)中的public成員在本類(lèi)中變成了protected的訪問(wèn)控制權(quán)限;private繼承,把父類(lèi)的public成員和protected成員在本類(lèi)中變成了private訪問(wèn)控制權(quán)。

  protected繼承:

  class Teacher : protected Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  class Teacher : protected Person

  {

  public:

  Teacher(const string& name, int age, const string& title)

  : Person(name, age), m_title(title)

  {

  }

  void ShowTeacherInfo()

  {

  ShowInfo(); /pic/p>

  cout << "職稱(chēng):" << m_title << endl; /pic/p>

  }

  private:

  string m_title; /pic/p>

  };

  void TestProtected()

  {

  Teacher teacher("李四", 35, "副教授");

  teacher.ShowInfo(); /pic/p>

  cout << endl;

  teacher.ShowTeacherInfo();

  }

  void TestProtected()

  {

  Teacher teacher("李四", 35, "副教授");

  teacher.ShowInfo(); /pic/p>

  cout << endl;

  teacher.ShowTeacherInfo();

  }

  class Leader : public Teacher

  {

  public:

  Leader(const string& name, int age, const string& title, string position)

  : Teacher(name, age, title), m_position(position)

  {

  }

  void ShowLeaderInfo()

  {

  ShowInfo(); /pic/p>

  ShowTeacherInfo(); /pic/p>

  cout << m_position << endl;

  }

  private:

  string m_position;

  };

  class Leader : public Teacher

  {

  public:

  Leader(const string& name, int age, const string& title, string position)

  : Teacher(name, age, title), m_position(position)

  {

  }

  void ShowLeaderInfo()

  {

  ShowInfo(); /pic/p>

  ShowTeacherInfo(); /pic/p>

  cout << m_position << endl;

  }

  private:

  string m_position;

  };


【C++ 的三種訪問(wèn)權(quán)限與三種繼承方式】相關(guān)文章:

C語(yǔ)言訪問(wèn)MCU寄存器的三種方式07-26

php遞歸函數(shù)三種方式02-21

理解Java訪問(wèn)權(quán)限的控制12-18

網(wǎng)絡(luò)中三種交換方式的比較02-10

Java編程里的包及訪問(wèn)權(quán)限01-07

關(guān)于C++中定義比較函數(shù)的三種方法12-19

路由器連接電腦的三種方式02-18

用CSS的float創(chuàng)建三欄布局的三種方式03-09

電腦無(wú)網(wǎng)絡(luò)訪問(wèn)權(quán)限怎么辦02-16