C++中继承的protected访问级别

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

 

2,根据面向对象理论:

 

    根据 C++ 语法:

  

3,继承中的访问级别编程实验:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Parent
 7 {
 8 private:
 9     int mv;
10     
11 public:
12     Parent()
13     {
14         mv = 100;
15     }
16     
17     int value()
18     {
19         return mv;
20     }
21 };
22 
23 class Child : public Parent  // Child 虽然是 Parent 的子类,但是严格意义上来说,它绝对是 Parent 的外部,因此 Parent 不可以访问 Parent 类的私有成员;
24 {
25 public:
26     int addValue(int v)
27     {
28         mv = mv + v;    // 这里访问了两次 mv,编译器则显示了两次编译错误,在同一个地方;如何访问父类的非公有成员?
29     }
30 };
31 
32 int main()
33 {   
34     return 0;
35 }

 

4,继承中的访问级别:

    1,面向对象中的访问级别不只是 public 和 private:

    2,可以定义 protected 访问级别;

    3,关键字 protected 的意义;

       1,修饰的成员不能被外界直接访问;

       2,修饰的成员可以被子类直接访问;

      

5,protected 初体验编程实验:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Parent
 7 {
 8 protected:
 9     int mv;
10 public:
11     Parent()
12     {
13         mv = 100;
14     }
15     
16     int value()
17     {
18         return mv;
19     }
20 };
21 
22 class Child : public Parent
23 {
24 public:
25     int addValue(int v)
26     {
27         mv = mv + v;    
28     }
29 };
30 
31 int main()
32 {   
33     Parent p;
34     
35     cout << "p.mv = " << p.value() << endl;  // 100;
36     
37     // p.mv = 1000;    // error,除子类之外的外界不能访问;
38     
39     Child c;
40     
41     cout << "c.mv = " << c.value() << endl;  // 100;
42     
43     c.addValue(50);  
44     
45     cout << "c.mv = " << c.value() << endl;  // 150;
46     
47     // c.mv = 10000;   // error,除子类之外的外界不能访问;
48     
49     return 0;
50 }

   

6,为什么面向对象中需要 protected ?

    1,所有的开发者应该都有一个架构师德梦想,架构师需要具备的一个素质就是设计能力,我们在做开发的时候,也许系统需要这个功能、也许不需要,究竟需不需要呢?一个系统中并不是功能越多越好,因此我们必须要有设计能,学习过程中要多思考;

    2,从生活中入手来思考,女孩子整容否、男孩子尿床、同性恋否,这些信息就是 private 的,生活中的工资是隐私,但是家人可以知道,这是就是 protected;

    3,protected 的引入是必须的、绝对的,没有它的引入,面向对象就是不完善的;

    4,其他的面向对象语言中如 Java、C# 等,它们当中的访问权限不止三种;

   

7,定义类时访问级别的选择:

 

    1,左右分支的不同之处在于,是否会被继承,这也是 protected 选择与否的根本依旧;

   

8,组合与继承的综合实例:

 

    1,这张图中 Object 这个类就是用来被继承的;

    2,Line 这个类组合了 Point 这个类;

    3,综合实例编程实验:

  1 #include <iostream>
  2 #include <string>
  3 #include <sstream>
  4 
  5 using namespace std;
  6 
  7 class Object  // 就是用来被继承的;
  8 {
  9 protected:  // 就是被继承
 10     string mName;
 11     string mInfo;
 12     
 13 public:
 14     Object()
 15     {
 16         mName = "Object";
 17         mInfo = "";
 18     }
 19     
 20     string name()
 21     {
 22         return mName;
 23     }
 24     
 25     string info()
 26     {
 27         return mInfo;
 28     }
 29 };
 30 
 31 class Point : public Object  // 点这个类不需要被继承;
 32 {
 33 private:
 34     int mX;
 35     int mY;
 36     
 37 public:
 38     Point(int x = 0, int y = 0)
 39     {
 40         ostringstream s;
 41         
 42         mX = x;
 43         mY = y;
 44         mName = "Point";
 45         
 46         s << "P(" << mX << ", " << mY << ")";
 47         
 48         mInfo = s.str();
 49     }
 50     
 51     int x()
 52     {
 53         return mX;
 54     }
 55     
 56     int y()
 57     {
 58         return mY;
 59     }
 60 };
 61 
 62 class Line : public Object  // 不会被继承;
 63 {
 64 private:
 65     Point mP1;
 66     Point mP2;
 67     
 68 public:
 69     Line(Point p1, Point p2)  // 这里编译错误,因为要调用默认构造函数,但是 Point 已经定义了构造函数,此刻可以采用赋默认值的方式;
 70     {
 71         ostringstream s;
 72         
 73         mP1 = p1;
 74         mP2 = p2;
 75         mName = "Line";
 76         
 77         s << "Line from " << mP1.info() << " to " << mP2.info();
 78         
 79         mInfo = s.str();
 80     }
 81     
 82     Point begin()
 83     {
 84         return mP1;
 85     }
 86     
 87     Point end()
 88     {
 89         return mP2;
 90     }
 91 };
 92 
 93 int main()
 94 {   
 95     Object o;
 96     Point p(1, 2);
 97     Point pn(5, 6);
 98     Line l(p, pn);
 99     
100     cout << o.name() << endl;  // 单元测试;Object
101     cout << o.info() << endl;  // 单元测试;“ ”
102     
103     cout << endl;
104     
105     cout << p.name() << endl;  // 单元测试;Point
106     cout << p.info() << endl;  // 单元测试;p(1, 2)
107     
108     cout << endl;
109     
110     cout << l.name() << endl;  // Line
111     cout << l.info() << endl;  // Line from p(1, 2) to p(5, 6)
112     
113     return 0;
114 }

 

9,小结:

    1,面向对象中的访问级别不只是 public 和 private;

    2,protected 修饰的成员不能被外界所访问;

    3,protected 使得子类能够访问父类的成员;

       1,子类继承父类后,子类和父类中相同的代码各有一套,分别存储使用的。

    4,protected 关键字是为了继承而专门设计的;

    5,没有 protected 就无法完成真正意义上的代码复用;

       1,因为 private 不能继承,而 public 不用继承,但是有些信息又是介乎两者之间的;

猜你喜欢

转载自www.cnblogs.com/dishengAndziyu/p/10914510.html
今日推荐