📄 protectedmember.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>受保护的(protected)成员</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++
Gossip: 受保护的(protected)成员</a></h1>
在之前的范例中,类别的资料成员多设定为private成员,也就是私用成员,私用成员只能在类别物件中使用,不能直接透过物件来呼叫使用,而即使是继承
了该类别的衍生类别也是如此,您只能透过该类别所提供的public函式成员来呼叫或设定私用成员。 <br>
<br>
然而有些时候,您希望继承了基底类别的衍生类别,能够直接存取呼叫基底类别中的成员,但不是透过public函式成员,也不是将它宣告为public,因
为您仍不希望这些成员被物件直接呼叫使用。 <br>
<br>
您可以宣告这些成员为“受保护的成员”(protected
member),保护的意思表示存取它有条件限制以保护该成员,当您将类别成员宣告为受保护的成员之后,继承它的类别就可以直接使用这些成员,但这些成员
仍然受到类别的保护,不可被物件直接呼叫使用。<br>
<br>
要宣告一个成员成为受保护的成员,就使用"protected"关键字,并将成员宣告于它的下方,下面这个程式是个实际的例子,您将资料成员宣告为受保护
的成员,继承它的类别就可以直接使用,而不用透过public函式成员来呼叫,这可以省去一些函式呼叫所带来的负担: <br>
<ul>
<li>Rectangle.h</li>
</ul>
<pre>class Rectangle { <br>public: <br> Rectangle() { <br> _x = 0;<br> _y = 0; <br> _width = 0;<br> _height = 0; <br> } <br><br> Rectangle(int x, int y, int width, int height)<br> : _x(x), _y(y), _width(width), _height(height){ <br> } <br><br> int x() { return _x; } <br> int y() { return _y; } <br> int width() { return _width; } <br> int height() { return _height; } <br> int area() { return _width*_height; } <br> <br>// 受保护的member <br>protected: <br> int _x;<br> int _y; <br> int _width;<br> int _height; <br>}; </pre>
<br>
<ul>
<li>Cubic.h</li>
</ul>
<pre>#include "Rectangle.h"<br><br>class Cubic : public Rectangle { <br>public: <br> Cubic() { <br> _z = 0; <br> _length = 0; <br> } <br><br> Cubic(int x, int y, int z, int length, int width, int height) <br> : Rectangle(x, y, width, height) , _z(z), _length(length) { <br> } <br><br> int length() { return _length; } <br> int volumn() { return _length*_width*_height; } <br><br>protected: <br> int _z; <br> int _length; <br>};</pre>
<span class="postbody">
</span><span class="postbody"><br>
</span>
<ul>
<li>main.cpp</li>
</ul>
<pre>#include <iostream><br>#include "Cubic.h"<br><br>using namespace std;<br><br>int main() {<br> Cubic c1(0, 0, 0, 10, 20, 30); <br><br> cout << "c1 volumn: " <br> << c1.volumn() <br> << endl; <br> <br> return 0;<br>}</pre>
<span class="postbody">
</span><span class="postbody"></span><span class="postbody"><br>
执行结果:</span><br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2"
cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span
style="color: rgb(255, 255, 255);">c1 volumn: 6000</span></small><span
style="color: rgb(255, 255, 255);"><br>
</span></td>
</tr>
</tbody>
</table>
<br>
在这个例子中,您可以看到直接使用继承下来的受保护成员确实比较方便,函式成员也可以宣告为受保护的成员,对象通常是仅适用于类别中使用的一些内部处理函
式,这些函式对类别外部来说,可能是呼叫它并没有意义或是有危险性,但您在衍生类别中仍可能使用到这些函式,所以通常会将之宣告为受保护的成员。 <br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -