值得思考的问题子类是否可以直接访问父类的私有成员呢?思考过程根据面向对象理论:根据C++ 语法:编程实验: 继承中的访问级别#include <iostream>using namespace std;class Parent{private: int mv;public: Parent() { mv = 100; } int value() { return mv; }};class Child : public Parent{public: int addvalue(int v) { mv = mv + v; // ??? 如何访问父类的非公有成员 ? }};int main(){ Child c; return 0;}输出:test.cpp: In member function ‘int Child::addvalue(int)’:test.cpp:8: error: ‘int Parent::mv’ is privatetest.cpp:26: error: within this contexttest.cpp:8: error: ‘int Parent::mv’ is privatetest.cpp:26: error: within this context继承中的访问级别面向对象中的访问级别不只是 public 和 private可以定义 protected 访问级别关键字 protected 的意义修饰的成员不能被外界直接访问修饰的成员可以被子类直接访问编程实验: protected 初体验#include <iostream>using namespace std;class Parent{protected: int mv;public: Parent() { mv = 100; } int value() { return mv; }};class Child : public Parent{public: int addvalue(int v) { mv = mv + v; }};int main(){ Parent p; cout << “p.mv = " << p.value() << endl; // p.mv = 1000; // Error Child c; cout << “c.mv = " << c.value() << endl; c.addvalue(50); cout << “c.mv = " << c.value() << endl; // c.mv = 1000; // Error return 0;}输出:p.mv = 100c.mv = 100c.mv = 150定义类时访问级别的选择组合与继承的综合实例point 继承于 ObjectLine 继承于 ObjectLine 包含两个 point编程实验: 综合实例#include <iostream>#include <string>#include <sstream>using namespace std;class Object{protected: string mName; string mInfo;public: Object() { mName = “Object”; mInfo = “”; } string name() { return mName; } string info() { return mInfo; }};class Point : public Object{private: int mX; int mY;public: Point(int x = 0, int y = 0) { mX = x; mY = y; mName = “Point”; ostringstream oss; oss << “p(” << mX << “,” << mY << “)”; mInfo = oss.str(); } int getX() { return mX; } int getY() { return mY; }};class Line : public Object{private: Point mP1; Point mP2;public: Line(Point p1, Point p2) { mP1 = p1; mP2 = p2; mName = “Line”; ostringstream oss; oss << “Line from " << mP1.info() << " to " << mP2.info(); mInfo = oss.str(); }};int main(){ Object o; cout << o.name() << endl; cout << o.info() << endl; cout << endl; Point p1(1, 2); Point p2(3, 4); cout << p1.name() << endl; cout << p1.info() << endl; cout << endl; Line l(p1, p2); cout << l.name() << endl; cout << l.info() << endl; return 0;}输出:ObjectPointp(1,2)LineLine from p(1,2) to p(3,4)小结面向对象中的访问级别不只是 public 和 privateprotected 修饰的成员不能被外界所访问protected 使得子类能够访问父类的成员protected 关键字是为了继承而专门设计的没有 protected 就无法完成真正意义上的代码复用以上内容参考狄泰软件学院系列课程,请大家保护原创!