上一篇文章我們介紹了c++中類的繼承學(xué)習(xí)總結(jié);今天我們繼續(xù)來分享c++中類的繼承中的訪問級(jí)別的學(xué)習(xí)總結(jié)。
一、繼承中的訪問級(jí)別學(xué)習(xí):
1、子類是否可以直接訪問父類的私用成員嗎?
從面向?qū)ο罄碚摻嵌葋砜矗?/p>
子類擁有父類的一切屬性和行為,也就是說,子類能夠直接訪問父類的私有成員。
從c++的語法角度看:
外界不能直接訪問類的private成員,也就是說,子類不能直接訪問父類的私用成員。
代碼示例:
#include <iostream>
#include <string>
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()
{
return 0;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: In member function ‘int Child::addValue(int)’:
test.cpp:9:9: error: ‘int Parent::mv’ is private
int mv;
^
test.cpp:27:9: error: within this context
mv = mv + v; // 如何訪問父類的非公有成員
^
test.cpp:9:9: error: ‘int Parent::mv’ is private
int mv;
^
test.cpp:27:14: error: within this context
mv = mv + v; // 如何訪問父類的非公有成員
^
注解:我們可以看到子類不能直接訪問到父類里面的屬性
2、繼承中的訪問級(jí)別關(guān)系
面向?qū)ο笾械脑L問級(jí)別不只是public和private
可以定義protected訪問級(jí)別
關(guān)鍵字protect的意義
--修飾的成員不能被外界直接訪問
-- 修飾的成員可以被子類直接訪問
代碼實(shí)現(xiàn)
#include <iostream>
#include <string>
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.a(chǎn)ddValue(50);
cout << "c.mv = " << c.value() << endl;
c.mv = 10000; // error
return 0;
}
運(yùn)行結(jié)果:
root@txp-virtual-machine:/home/txp# g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:9:9: error: ‘int Parent::mv’ is protected
int mv;
^
test.cpp:37:8: error: within this context
p.mv = 1000; // error
^
test.cpp:9:9: error: ‘int Parent::mv’ is protected
int mv;
^
test.cpp:47:7: error: within this context
c.mv = 10000; // error
注解:這里我們把父類的屬性private修改成protect,這里我們注意到在子類里面的方法中是可以使用父類中的屬性mv了,只不過在int main()函數(shù)中,使用父類和子類定義的對(duì)象,均不可以對(duì)父類中的屬性mv進(jìn)行訪問,這一點(diǎn)要注意。
3、為什么面向?qū)ο笾行枰猵rotect?
我們還是用生活中的例子來理解,每個(gè)人的個(gè)人隱私,是不能泄露的,也就是我們c++中的private關(guān)鍵字;而你身上穿的衣服,每個(gè)人都可以知道,也就是c++中的public關(guān)鍵字;最后我們的protect關(guān)鍵字,為啥c++中會(huì)需要它,我想還是跟生活中有關(guān)(所以說,面向?qū)ο蟮?a target="_blank">編程,非常貼近生活),比如說,家庭開會(huì),有些事情就不能讓外人知道,但是自己家人就可以知道,所以這跟protect關(guān)鍵字的用法非常像,也就是說,protect關(guān)鍵鑒于private和public之間。
4、定義類時(shí)訪問級(jí)別的選擇:
注解:從圖中我們可以發(fā)現(xiàn),當(dāng)有發(fā)生繼承關(guān)系時(shí),就考慮使用protect關(guān)鍵字
5、組合和繼承的綜合運(yùn)用
說明:Object這個(gè)類是被用來繼承的;Line和Point兩個(gè)類進(jìn)行組合。
代碼示例:
#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)
{
ostringstream s;
mX = x;
mY = y;
mName = "Point";
s << "P(" << mX << ", " << mY << ")";
mInfo = s.str();
}
int x()
{
return mX;
}
int y()
{
return mY;
}
};
class Line : public Object
{
private:
Point mP1;
Point mP2;
public:
Line(Point p1, Point p2)
{
ostringstream s;
mP1 = p1;
mP2 = p2;
mName = "Line";
s << "Line from " << mP1.info() << " to " << mP2.info();
mInfo = s.str();
}
Point begin()
{
return mP1;
}
Point end()
{
return mP2;
}
};
int main()
{
Object o;
Point p(1, 2);
Point pn(5, 6);
Line l(p, pn);
cout << o.name() << endl;
cout << o.info() << endl;
cout << endl;
cout << p.name() << endl;
cout << p.info() << endl;
cout << endl;
cout << l.name() << endl;
cout << l.info() << endl;
return 0;
}
輸出結(jié)果:
root@txp-virtual-machine:/home/txp# ./a.out
Object
Point
P(1, 2)
Line
Line from P(1, 2) to P(5, 6)
二、總結(jié):
面向?qū)ο笾械脑L問級(jí)別不只是public和private
protected修飾的成員不能別外界所訪問
protected使得子類能夠訪問父類的成員
protected關(guān)鍵字為了繼承而專門設(shè)計(jì)的
沒有protected關(guān)鍵字就無法完成真正代碼意義上的代碼復(fù)用了
好了,今天的分享就到這里,如果文章中有錯(cuò)誤或者不理解的地方,可以交流互動(dòng),一起進(jìn)步。我是txp,下期見!
-
可編程邏輯
+關(guān)注
關(guān)注
7文章
526瀏覽量
44727 -
C++
+關(guān)注
關(guān)注
22文章
2119瀏覽量
75186
發(fā)布評(píng)論請(qǐng)先 登錄
C++學(xué)到什么程度可以找工作?
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類庫案例:ThreadPoll

Spire.XLS for C++組件說明

EE-112:模擬C++中的類實(shí)現(xiàn)

ostream在c++中的用法
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類庫案例:SafeQueue

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類庫案例:SafeStack

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類庫案例:SafeBlockQueue

基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類庫案例:SafeStack
基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)的C++公共基礎(chǔ)類庫案例:SafeQueue
java子類可以繼承父類的什么
OpenHarmony標(biāo)準(zhǔn)系統(tǒng)C++公共基礎(chǔ)類庫案例:HelloWorld

HarmonyOS Next原生應(yīng)用開發(fā)-從TS到ArkTS的適配規(guī)則(八)
C++中實(shí)現(xiàn)類似instanceof的方法

評(píng)論