类中的权限概念:
1. 类和结构体中最大的区别就是权限问题。结构体没有权限标识关键字,默认为public。类中则增加了public、private、protected关键字;
2. public为最公开的权限,所有属性方法本类的对象和指针都可以调用;
3. private为私有权限,只有在本类中可以使用;
4.protected为保护权限,只有在本类和子类中使用;
<span style="font-size:18px;">#include <stdio.h>
#include <iostream>
using namespace std;
class Father
{
public:
Father()
{
nParaA = 1;
nParaB = 2;
nParaC = 0;
Run();
}
~Father(){}
public:
int MaxFunc(int paraA, int paraB)
{
return (paraA>paraB?paraA:paraB);
}
void Run()
{
nParaC = MaxFunc(nParaA,nParaB);
}
private://私有时,只能在本类中使用
int nParaA;
int nParaB;
int nParaC;
};
class SubClass : public Father
{
public:
SubClass()
{
nSub = 0;
//nSub = nParaC;//当nParaC为public或protected时,可直接使用
}
~SubClass(){}
int nSub;
};
int main()
{
// Father* pFather = new SubClass();//如果父类中nParaC为public,则可访问
// pFather->Run();
// cout << pFather->nParaC<< endl;
// SubClass a;
// cout << a.nSub<< endl;</span>
<span style="font-size:18px;">
// Father* pFather = new Father;</span>
<span style="font-size:18px;">// cout << pFather->nParaC << endl;</span>
<span style="font-size:18px;">
system("pause");
return 0;
}</span>