被忽略的细节:冒号 ( : ) 表示继承关系,Parent 表示被继承的类, public 的意义是什么呢?class Parent{};class Child : public Parent{};有趣的问题是否可以将继承语句中的 public 换成 protected 或者 private ? 如果可以,与 public 继承有什么区别?编程实验: 有趣的尝试#include <iostream>using namespace std;class Parent{};class Child_A : public Parent{};class Child_B : protected Parent{};class Child_C : private Parent{};int main(){ return 0;}编译无错误,无警告不同的继承方式C++ 中支持三种不同的继承方式public 继承父类成员在子类中保持原有的访问级别private 继承父类成员在子类中变为私有继承protected 继承父类中的公有成员变为保护成员,其它成员保持不变继承成员的访问属性 := Max {继承方式, 父类成员访问属性}C++ 中的默认继承方式为 privateclass Child : Parent {};<–>class Child : Private Parent {};编程实验: 继承与访问级别深度实践#include <iostream>using namespace std;class Parent{protected: int m_a;protected: int m_b;public: int m_c; void set(int a, int b, int c) { m_a = a; m_b = b; m_c = c; }};class Child_A : public Parent{public: void print() { cout << “m_a = " << m_a << endl; cout << “m_b = " << m_a << endl; cout << “m_c = " << m_a << endl; }};class Child_B : protected Parent{public: void print() { cout << “m_a = " << m_a << endl; cout << “m_b = " << m_a << endl; cout << “m_c = " << m_a << endl; }};class Child_C : private Parent{public: void print() { cout << “m_a = " << m_a << endl; cout << “m_b = " << m_a << endl; cout << “m_c = " << m_a << endl; }};int main(){ Child_A a; Child_B b; Child_C c; a.m_c = 100; // b.m_c = 100; // Child_B 保护继承自 Parent ,所以所有的 public 成员全部变成了 protected 成员,因此外界无法访问 // c.m_c = 100; // Child_C 私有继承自 Parent ,所以所有的成员全部变成了 private 成员,因此外界无法访问 a.set(1, 1, 1); // b.set(2, 2, 2); // c.set(3, 3, 3); a.print(); b.print(); c.print(); return 0;}输出:m_a = 1m_b = 1m_c = 1m_a = 134515136m_b = 134515136m_c = 134515136m_a = 134515161m_b = 134515161m_c = 134515161遗憾的事实一般而言, C++ 工程中只使用 public 继承C++ 的派生语言只支持一种继承方式(public继承)protected 和 private 继承带来的复杂性远大于实用性编程实验: C++ 派生语言初探test.dmodule D_Demo;import std.stdio;import std.string;class Obj{protected: string mName; string mInfo;public: this() { mName = “Object”; mInfo = “”; } string name() { return mName; } string info() { return mInfo; }}class Point : Obj{private: int mX; int mY;public: this(int x, int y) { mX = x; mY = y; mName = “Point”; mInfo = format(“P(%d, %d)”, mX, mY); } int x() { return mX; } int y() { return mY; }}void main(string[] args){ writefln(“D Demo”); Point p = new Point(1, 2); writefln(p.name()); writefln(p.info());}输出:D DemoPointP(1, 2)test.csclass Obj{ protected string mName; protected string mInfo; public Obj() { mName = “Object”; mInfo = “”; } public string name() { return mName; } public string info() { return mInfo; }}class Point : Obj{ private int mX; private int mY; public Point(int x, int y) { mX = x; mY = y; mName = “Point”; mInfo = “P(” + mX + “, " + mY + “)”; } public int x() { return mX; } public int y() { return mY; }}class Program{ public static void Main(string[] args) { System.Console.WriteLine(“C# Demo”); Point p = new Point(1, 2); System.Console.WriteLine(p.name()); System.Console.WriteLine(p.info()); }}输出:C# DemoPointP(1, 2)test.javaclass Obj{ protected String mName; protected String mInfo; public Obj() { mName = “Object”; mInfo = “”; } public String name() { return mName; } public String info() { return mInfo; }}class Point extends Obj{ private int mX; private int mY; public Point(int x, int y) { mX = x; mY = y; mName = “Point”; mInfo = “P(” + mX + “, " + mY + “)”; } public int x() { return mX; } public int y() { return mY; }}class Program{ public static void main(String[] args) { System.out.println(“Java Demo”); Point p = new Point(1, 2); System.out.println(p.name()); System.out.println(p.info()); }}输出:Java DemoPointP(1, 2)你有没有读懂呢?学习 C++ 的魅力(意义)!小结C++ 中支持三种不同的继承方式继承方式直接影响父类成员在子类中的访问属性一般而言,工程中只使用 public 的继承方式C++ 的派生语言只支持 public 继承方式以上内容参考狄泰软件学院系列课程,请大家保护原创!