继承中的访问级别

  • 问题:子类是否可以直接访问父类的私有成员(private)?
  • 问题的思考过程:
  1. 根据面向对象理论:子类拥有父类的一切属性和行为=====》子类能够直接访问父类的私有成员!
  2. 根据C++语法:外界不能直接访问类的private成员  =====》子类不能直接访问父类的私有成员!
  • 实验
 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 using namespace std;
 5  
 6  
 7 class Parent
 8 {
 9 private:
10     int mv;
11 public:
12     Parent()
13     {
14         mv = 100;
15     }
16 };
17  
18  
19 class Child : public Parent
20 {
21 public:
22     int addval()
23     {
24         //子类无法访问父类的private成员
25         int v = mv + 100;
26         return v;
27     }
28 };
29  
30 int main()
31 {
32     Parent p;
33     
34     Child c;
35     c.mv = 1000;  //error,类的外部是不能直接访问protected成员的
36     cout << "c.addval()=" << c.addval() << endl;
37 }
编译报错,子类访问不了父类的private成员
  • 新知识:
  1. 面向对象中的访问级别不只是public和private
  2. 可以定义protected成员
  3. 关键字protected的意义:修饰的成员不能被外界直接访问
  4. 修饰的成员可以被子类直接访问
  • protected实验:
 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 using namespace std;
 5  
 6  
 7 class Parent
 8 {
 9 protected:
10     int mv;
11 public:
12     Parent()
13     {
14         mv = 100;
15     }
16 };
17  
18  
19 class Child : public Parent
20 {
21 public:
22     int addval()
23     {
24         //子类可以访问父类的protected成员
25         int v = mv + 100;
26         return v;
27     }
28 };
29 int main()
30 {
31     Parent p;
32     
33     Child c;
34     //c.mv = 1000;  //error,类的外部是不能直接访问protected成员的
35     cout << "c.addval()=" << c.addval() << endl;
36 }
  • 运行结果:
  • 思考问题:为什么对象中需要protected?
          坛友举了一个例子,很形象: https://blog.csdn.net/feng__shuai/article/details/90906368
  • 定义类时访问级别的选择:
 
  • 综合实例:
  1 // 继承中的访问级别.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2 //
  3 #include <iostream>
  4 #include <string>
  5 #include <sstream>
  6 using namespace std;
  7 class Parent
  8 {
  9 protected:
 10     int mv;
 11 public:
 12     Parent()
 13     {
 14         mv = 100;
 15     }
 16 };
 17 class Child : public Parent
 18 {
 19 public:
 20     int addval()
 21     {
 22         //子类无法访问父类的private成员
 23         int v = mv + 100;
 24         return v;
 25     }
 26 };
 27 class object
 28 {
 29 protected:
 30     string mName;
 31     string mInfo;
 32 public:
 33     object()
 34     {
 35         mName = "object";
 36         mInfo = "";
 37     }
 38     string get_mName()
 39     {
 40         return mName;
 41     }
 42     string get_mInfo()
 43     {
 44         return mInfo;
 45     }
 46 };
 47 class point : public object
 48 {
 49 protected:
 50     int mx;
 51     int my;
 52 public:
 53     point(int x = 0,int y = 0)
 54     {
 55         ostringstream s;
 56         mx = x;
 57         my = y;
 58         //在子类中可以给父类的protected成员赋值
 59         mName = "point";
 60         s << "P(" << mx << "," << my << ")";
 61         mInfo = s.str();
 62     }
 63     int get_x()
 64     {
 65         return mx;
 66     }
 67     int get_y()
 68     {
 69         return my;
 70     }
 71 };
 72 class Line : public object
 73 {
 74 private:
 75     point mp1;
 76     point mp2;
 77 public:
 78     Line(point p1,point p2)
 79     {
 80         ostringstream s;
 81         mp1 = p1;
 82         mp2 = p2;
 83         mName = "Line";
 84         s << "Line From " << mp1.get_mInfo() << "to" << mp2.get_mInfo() << endl;
 85         mInfo = s.str();
 86     }
 87 };
 88 int main()
 89 {
 90     Parent p;
 91    // cout << "p.mv" << p.mv << endl; error,只有子类才可以访问protected成员
 92     
 93     Child c;
 94    // c.mv = 1000;  error,类的外部是不能直接访问protected成员的
 95     cout << "c.addval()=" << c.addval() << endl;
 96     object o;
 97     cout << o.get_mName() << endl;
 98     cout << o.get_mInfo() << endl;
 99     point point1(1,2);
100     cout << point1.get_mName() << endl;
101     cout << point1.get_mInfo() << endl;
102     point point2(5, 6);
103     Line L(point1, point2);
104     cout << L.get_mName() << endl;
105     cout << L.get_mInfo() << endl;
106 }
  • 运行结果:
c.addval()=200
object
 
point
P(1,2)
Line
Line From P(1,2)toP(5,6)
  • 小结:
  1. 面向对象中的访问级别不只是public和private
  2. protected修饰的成员不能被外界所访问
  3. protected使得子类能够访问父类的成员
  4. protected关键字是为了继承而专门设计的
  5. 没有protected就无法完成真正意义上的代码复用

猜你喜欢

转载自www.cnblogs.com/chengeputongren/p/12241184.html
今日推荐