C++中public、protected、private三种访问说明符
一 充当类成员的访问说明符,加强类的封装性
public成员:一切都可以访问;
protected成员:只可以被派生类、本身访问;
private成员:只可以被本身访问;
友元:友元是例外,破坏了类的封装性,可以访问类的所有成员;
举例:
#include <iostream>
using namespace std;
class Base
{
friend class Friend; // 声明Friend为友元类
public:
Base(int a, int b, int c) : m_nPublic(a), m_nProtected(b), m_nPrivate(c) { }
int m_nPublic;
protected:
int m_nProtected;
private:
int m_nPrivate;
};
class Friend
{
public:
Friend(int a, int b, int c) : m_cBase(a, b , c) { }
void print()
{
// 友元能访问类的所有成员
cout << "Base::m_nPublic = " << m_cBase.m_nPublic << endl;
cout << "Base::m_nProtected = " << m_cBase.m_nProtected << endl;
cout << "Base::m_nPrivate = " << m_cBase.m_nPrivate << endl;
}
private:
Base m_cBase;
};
int main()
{
Base b(1,2,3);
cout << "Base::m_nPublic = " << b.m_nPublic << endl;
// cout << "Base::m_nProtected = " << b.m_nProtected << endl; //error,无访问权限
// cout << "Base::m_nPrivate = " << b.m_nPrivate << endl; //error,无访问权限
Friend f(1, 2, 3);
f.print();
}
输出结果:
Base::m_nPublic = 1
Base::m_nPublic = 1
Base::m_nProtected = 2
Base::m_nPrivate = 3
二 充当基类的访问说明符,实现OOP中的继承
public继承:基类成员的访问属性在派生类中保持不变,基类的public、protected 在派生类中依旧是 public、protected;
protected继承:基类的public、protected 在派生类中是 protected、protected;
private继承:基类的public、protected 在派生类中是 private、private;
举例:public继承
class Derived : public Base
{
public:
Derived(int a, int b, int c) : Base(a, b, c) { }
void Print()
{
cout << "Base::m_nPublic = " << Base::m_nPublic << endl;
cout << "Base::m_nProtected = " << Base::m_nProtected << endl;
// cout << "Base::m_nPrivate = " << Base::m_nPrivate << endl; //error,无访问权限
}
};
int main()
{
Derived d(1, 2, 3);
d.Print();
cout << endl;
cout << "Base::m_nPublic = " << d.m_nPublic << endl;
// cout << "Base::m_nProtected = " << d.m_nProtected << endl; //error,无访问权限
}
输出结果:
Base::m_nPublic = 1
Base::m_nProtected = 2
Base::m_nPublic = 1
举例:protected继承
class Derived : protected Base
{
public:
Derived(int a, int b, int c) : Base(a, b, c) { }
void Print()
{
cout << "Base::m_nPublic = " << Base::m_nPublic << endl;
cout << "Base::m_nProtected = " << Base::m_nProtected << endl;
// cout << "Base::m_nPrivate = " << Base::m_nPrivate << endl; //error,无访问权限
}
};
int main()
{
Derived d(1, 2, 3);
d.Print();
cout << endl;
// cout << "Base::m_nPublic = " << d.m_nPublic << endl; //error,无访问权限
// cout << "Base::m_nProtected = " << d.m_nProtected << endl; //error,无访问权限
}
输出结果:
Base::m_nPublic = 1
Base::m_nProtected = 2
举例:private继承
class Derived : private Base
{
public:
Derived(int a, int b, int c) : Base(a, b, c) { }
void Print()
{
cout << "Base::m_nPublic = " << Base::m_nPublic << endl;
cout << "Base::m_nProtected = " << Base::m_nProtected << endl;
// cout << "Base::m_nPrivate = " << Base::m_nPrivate << endl; //error,无访问权限
}
};
int main()
{
Derived d(1, 2, 3);
d.Print();
cout << endl;
// cout << "Base::m_nPublic = " << d.m_nPublic << endl; //error,无访问权限
// cout << "Base::m_nProtected = " << d.m_nProtected << endl; //error,无访问权限
}
输出结果:
Base::m_nPublic = 1
Base::m_nProtected = 2