1.值得思考的问题

子类是否可以直接访问父类的私有成员?

根据面向对象理论:
子类拥有父类一切属性和行为—->子类能够直接访问父类的私有成员

根据c++语法:
外界不能直接访问类的private成员—->子类不能直接访问父类的私有成员

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Parent
  5. {
  6. int mv;
  7. public:
  8. Parent()
  9. {
  10. mv = 100;
  11. }
  12. int Value()
  13. {
  14. return mv;
  15. }
  16. };
  17. class Child : public Parent
  18. {
  19. public:
  20. int addValue(int v)
  21. {
  22. mv += v; //how to interview the parent class's private member
  23. }
  24. };
  25. int main()
  26. {
  27. return 0;
  28. }

2.继承中的访问级别

—-面向对象的访问级别不只是public和private
—-可以定义protected访问级别
—-关键字protected的意义
被protected修饰的成员不能被外界直接访问,但是可以被子类访问

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Parent
  5. {
  6. protected:
  7. int mv;
  8. public:
  9. Parent()
  10. {
  11. mv = 100;
  12. }
  13. int Value()
  14. {
  15. return mv;
  16. }
  17. };
  18. class Child : public Parent
  19. {
  20. public:
  21. int addValue(int v)
  22. {
  23. mv += v; //how to interview the parent class's private member
  24. }
  25. };
  26. int main()
  27. {
  28. Parent p;
  29. Child c;
  30. cout << "p.mv = " << p.Value() << endl;
  31. //p.mv = 1000; //error
  32. cout << "c.mv = " << c.Value() << endl;
  33. c.addValue(50);
  34. cout << "c.mv = " << c.Value() << endl;
  35. //c.mv = 10000; //error
  36. return 0;
  37. }

3.为什么面向对象中需要protected?

保护私有信息

4.定义类时访问级别的选择

Slide8.PNG

5.组合与继承的实例分析Slide9.PNG

  1. #include <iostream>
  2. #include <string>
  3. #include<sstream>
  4. using namespace std;
  5. class Object
  6. {
  7. protected:
  8. string mName;
  9. string mInfo;
  10. public:
  11. Object()
  12. {
  13. mName = "Object";
  14. mInfo = "";
  15. }
  16. string name()
  17. {
  18. return mName;
  19. }
  20. string info()
  21. {
  22. return mInfo;
  23. }
  24. };
  25. class Point :public Object
  26. {
  27. private:
  28. int mx;
  29. int my;
  30. public:
  31. Point(int x = 0, int y = 0)
  32. {
  33. ostringstream s;
  34. mx = x;
  35. my = y;
  36. mName = "Point";
  37. mInfo = "";
  38. s << "P(" << mx <<","<< my << ")";
  39. mInfo = s.str();
  40. }
  41. int x()
  42. {
  43. return mx;
  44. }
  45. int y()
  46. {
  47. return my;
  48. }
  49. };
  50. class Line : public Object
  51. {
  52. private:
  53. Point mP1;
  54. Point mP2;
  55. public:
  56. Line(Point p1, Point p2)
  57. {
  58. ostringstream s;
  59. mP1 = p1;
  60. mP2 = p2;
  61. mName = "Line";
  62. s << "Line from " << mP1.info() << "to" << mP2.info();
  63. mInfo = s.str();
  64. }
  65. };
  66. int main()
  67. {
  68. Object o;
  69. Point p(1, 2);
  70. Point pn(5, 6);
  71. Line l(p, pn);
  72. cout << o.name() << endl;
  73. cout << o.info() << endl;
  74. cout << p.name() << endl;
  75. cout << p.info() << endl;
  76. cout << l.name() << endl;
  77. cout << l.info() << endl;
  78. return 0;
  79. }

小结

1.面向对象中的访问级别不只是public和private
2.protected修饰的成员不能被外界所访问
3.protected使得子类能够访问父类的成员
4.protected关键字是为了继承而专门设计的
5.没有protected就无法完成真正意义上的代码复用