對于初學者C++的屬性控制以及繼承屬性還是比較那掌握的,筆者在這裡總結一下自己學習這些過程的見解。首先我們來看:
1、C++支援的通路屬性有public、protected、private,下來我淺談一下我在學習過程中的一些領悟。
1、對于public
1、該類中的函數、子類中的函數、其友元函數的通路以及該類中的對象
2、我個人一直覺得用代碼了解概念是一個很好的方法,下邊我将給出一些我的測試用例:
class Base
{
public:
//友元函數
friend void friend_Show(Base &bb);
public:
Base():x(0),px(0){ }
~Base(){ }
public:
//公有屬性
int px;
void ShowBase()
{
cout<<"I am Show Base and am public"<<endl;
}
void Test()
{
cout<<"I am Test"<<endl;
//測試保護方法調用公有方法成功了嗎
Print();
//測試公有屬性可以被保護方法修改嗎
cout<<"px = "<<px<<endl;
//測試私有方法調用公有方法
Print_Private();
//測試私有方法修改公有屬性
cout<<"px = "<<px<<endl;
}
protected:
void Print()
{
cout<<"I am Print "<<endl;
//公有方法被保護成員通路
ShowBase();
//保護方法修改公有屬性
px = 10;
}
private:
void Print_Private()
{
cout<<"I am Pint_Private"<<endl;
//公有成員被私有成員通路
ShowBase();
//私有方法修改公有屬性
px = 20;
}
private:
int x;
};
void friend_Show(Base &bb)
{
cout<<"I am friend_Show"<<endl;
//友元方法通路公有屬性
bb.px= 30;
////友元方法通路公有方法
bb.ShowBase();
}
void main()
{
Base bb;
bb.Test();
//測試友元方法方法公有屬性和方法
friend_Show(bb);
cout<<"bb.px = "<<bb.px<<endl;
}
運作結果:
上邊的測試代碼我給出了盡可能多注釋,驗證了上邊幾個屬性,除了子類中的函數的通路屬性沒有給出測試用例,其他的都給出了測試用例。子類中的函數的通路屬性我下邊将作為一個專門的子產品總結。
2、對于ptotected
1、可以被類中的函數、子類中的函數、其友元函數、該類的對象通路
2、這裡我同樣給出測試代碼:
class Base
{
public:
//友元函數
friend void friend_Show(Base &bb);
public:
Base():x(0),pp(0) { }
~Base() { }
public:
void ShowBase()
{
cout<<"I am Show Base and am public"<<endl;
//調用保護屬性
Print();
//修改保護屬性
pp = 40;
}
void Test()
{
cout<<"I am Test"<<endl;
//測試保護方法調用保護方法
Test_proteced();
//測試公保護方法修改保護屬性
cout<<"pp = "<<pp<<endl;
//測試私有方法調用保護方法
Print_Private();
//測試私有方法修改保護屬性
cout<<"pp = "<<pp<<endl;
//測試公有方法調用保護方法
ShowBase();
//測試公有方法修改保護屬性
cout<<"pp = "<<pp<<endl;
}
void TestFriend()
{
cout<<"I am TestFriend"<<endl;
cout<<"pp = "<<pp<<endl;
}
protected:
int pp;
void Print()
{
cout<<"I am Print and am protected "<<endl;
}
void Test_proteced()
{
cout<<"I am Test_proteced "<<endl;
//測試保護方法調用
Print();
pp = 50;
}
private:
void Print_Private()
{
cout<<"I am Pint_Private"<<endl;
//公有成員被私有成員通路
Print();
//私有方法修改保護屬性
pp = 60;
}
private:
int x;
};
void friend_Show(Base &bb)
{
cout<<"I am friend_Show"<<endl;
//友元方法通路保護屬性
bb.pp= 30;
////友元方法通路保護方法
bb.Print();
}
void main()
{
Base bb;
bb.Test();
//測試友元方法方法公有屬性和方法
friend_Show(bb);
bb.TestFriend();
}
同樣我在代碼中給出了盡可能詳盡的注釋,對除了子函數中的通路沒有給出,子函數的通路權限後邊我将作為一個專門的子產品總結。
3、對于private修飾符
1、隻能有該類中的函數以及其友元函數通路,不能被任何其他函數和對象通路。
2、同樣我也給出測試代碼:
class Base
{
public:
//友元函數
friend void friend_Show(Base &bb);
public:
Base():x(0) { }
~Base() { }
public:
void ShowBase()
{
cout<<"I am Show Base and am public"<<endl;
//調用私有屬性
Print_Private();
//修改私有屬性
x = 200;
}
void Test()
{
cout<<"I am Test"<<endl;
//測試公有方法調用保護方法
ShowBase();
//測試公有方法修改私有屬性
cout<<"x = "<<x<<endl;
//測試保護方法調用私有方法
Print();
//測試保護方法修改私有屬性
cout<<"x = "<<x<<endl;
//測試私有方法調用私有方法
Test_Private();
//測試是由私有方法修改私有屬性
cout<<"x = "<<x<<endl;
}
void TestFriend()
{
cout<<"I am TestFriend"<<endl;
cout<<"x = "<<x<<endl;
}
protected:
void Print()
{
cout<<"I am Print and am protected "<<endl;
//測試通路私有屬性的方法
Print_Private();
//測試調用屬性的屬性
x = 100;
}
private:
void Print_Private()
{
cout<<"I am Pint_Private"<<endl;
}
void Test_Private()
{
cout<<"I am Test_priavate"<<endl;
Print_Private();
}
private:
int x;
};
void friend_Show(Base &bb)
{
cout<<"I am friend_Show"<<endl;
//友元方法通路私有屬性
bb.x= 300;
////友元方法通路私有方法
bb.Print_Private();
}
void main()
{
Base bb;
bb.Test();
//測試友元方法方法公有屬性和方法
friend_Show(bb);
bb.TestFriend();
}
最後也同樣給出了privete通路控制測試代碼以及截圖,驗證了我上邊給出的通路控制權限。