C++三种继承方式

C++三种继承方式教程

C++ 中的 继承 有三种方式,即 public、protected 和 private,此项是可选项,如果不写,默认为 private,也就是 成员变量成员函数 默认也是 private。

public protected private继承方式区别

说明

不同的继承方式会影响基类成员在派生类中的访问权限。

public继承方式

  • 基类中所有 public 成员在派生类中为 public 属性;
  • 基类中所有 protected 成员在派生类中为 protected 属性;
  • 基类中所有 private 成员在派生类中不能使用。

protected继承方式

  • 基类中的所有 public 成员在派生类中为 protected 属性;
  • 基类中的所有 protected 成员在派生类中为 protected 属性;
  • 基类中的所有 private 成员在派生类中不能使用。

private继承方式

  • 基类中的所有 public 成员在派生类中均为 private 属性;
  • 基类中的所有 protected 成员在派生类中均为 private 属性;
  • 基类中的所有 private 成员在派生类中不能使用。

总结

继承方式/基类成员 public成员 protected成员 private成员
public继承 public protected 不可见
protected继承 protected protected 不可见
private继承 private private 不可见

public protected private继承方式详解

在 C++ 中,基类成员在派生类中的访问权限不得高于继承方式中指定的权限。例如,当继承方式为 protected 时,那么基类成员在派生类中的访问权限最高也为 protected,高于 protected 的会降级为 protected,但低于 protected 不会升级。再如,当继承方式为 public 时,那么基类成员在派生类中的访问权限将保持不变。

也就是说,继承方式中的 public、protected、private 是用来指明基类成员在派生类中的最高访问权限的。不管继承方式如何,基类中的 private 成员在派生类中始终不能使用(不能在派生类的成员函数中访问或调用)。

如果希望基类的成员能够被派生类继承并且毫无障碍地使用,那么这些成员只能声明为 public 或 protected;只有那些不希望在派生类中使用的成员才声明为 private。如果希望基类的成员既不向外暴露(不能通过对象访问),还能在派生类中使用,那么只能声明为 protected。

注意,我们这里说的是基类的 private 成员不能在派生类中使用,并没有说基类的 private 成员不能被继承。实际上,基类的 private 成员是能够被继承的,并且(成员变量)会占用派生类对象的内存,它只是在派生类中不可见,导致无法使用罢了。private 成员的这种特性,能够很好的对派生类隐藏基类的实现,以体现面向对象的封装性。

案例

C++ public protected private继承

C++ public protected private继承

#include <iostream> #include <cstring> using namespace std; //人类 class Person { public: void setName(string name) { this->m_name = name; } void setAge(int age) { this->m_age = age; } void setHeight(int height) { this->m_height = height; } void info() { cout << "Name = " << this->m_name << " Age = " << this->m_age << " Height = " << this->m_height << endl; } public: string m_name; protected: int m_age; private: int m_height; }; //学生类 class Student : public Person { }; int main(void) { cout << "嗨客网(www.haicoder.net)\n" << endl; Student stu; stu.setName("HaiCoder"); stu.setAge(110); stu.setHeight(173); stu.info(); return 0; }

程序运行后,控制台输出如下:

02_C public protected private继承.png

我们首先,定义了一个基类 Person,接着,我们定义了一个派生类 Student,其继承自基类 Person,同时,我们使用了 public 继承。

最后,在 main 函数里面,我们可以直接调用了 Person 类里面的 public 方法,此时程序运行正常,现在,我们修改程序,在派生类中直接访问基类的成员,修改程序如下:

#include <iostream> #include <cstring> using namespace std; //人类 class Person { public: void setName(string name) { this->m_name = name; } void setAge(int age) { this->m_age = age; } void setHeight(int height) { this->m_height = height; } void info() { cout << "Name = " << this->m_name << " Age = " << this->m_age << " Height = " << this->m_height << endl; } public: string m_name; protected: int m_age; private: int m_height; }; //学生类 class Student : public Person { public: void show() { cout << "Stu Name = " << this->m_name << " Age = " << this->m_age << " Height = " << this->m_height << endl; } }; int main(void) { cout << "嗨客网(www.haicoder.net)\n" << endl; Student stu; stu.setName("HaiCoder"); stu.setAge(110); stu.setHeight(173); stu.show(); return 0; }

程序运行后,控制台输出如下:

03_C public protected private继承.png

我们在派生类 Student 里面直接访问了基类 Person 的成员,此时,程序报错,因为 private 成员只能在本类访问,现在,我们再次修改程序,修改为 private 继承,修改程序如下:

#include <iostream> #include <cstring> using namespace std; //人类 class Person { public: void setName(string name) { this->m_name = name; } void setAge(int age) { this->m_age = age; } void setHeight(int height) { this->m_height = height; } void info() { cout << "Name = " << this->m_name << " Age = " << this->m_age << " Height = " << this->m_height << endl; } public: string m_name; protected: int m_age; private: int m_height; }; //学生类 class Student : private Person { }; int main(void) { cout << "嗨客网(www.haicoder.net)\n" << endl; Student stu; stu.setName("HaiCoder"); stu.setAge(110); stu.setHeight(173); stu.info(); return 0; }

程序运行后,控制台输出如下:

04_C public protected private继承.png

这次,我们将继承方式修改为了 private,我们可以看到,这次,我们不可以访问基类里面的任何成员了。

C++ public protected private继承方式区别总结

继承方式中的 public、protected、private 是用来指明基类成员在派生类中的最高访问权限的。不管继承方式如何,基类中的 private 成员在派生类中始终不能使用(不能在派生类的成员函数中访问或调用)。

如果希望基类的成员能够被派生类继承并且毫无障碍地使用,那么这些成员只能声明为 public 或 protected;只有那些不希望在派生类中使用的成员才声明为 private。如果希望基类的成员既不向外暴露(不能通过对象访问),还能在派生类中使用,那么只能声明为 protected。