C++ 继承中的访问控制
在本教程中,我们将通过示例学习在 C++ 中使用公共、受保护和私有继承。
在C++继承中,我们可以在不同的访问模式下从基类派生一个子类。例如,
class Base {
.... ... ....
};
class Derived : public Base {
.... ... ....
};
注意代码中的 public
关键字
class Derived : public Base
这意味着我们已经在公共模式下从基类创建了一个派生类。同样,我们也可以在受保护或私有模式下派生类。
这 3 个关键字( public
、 protected
和 private
)在 C++ 继承中称为访问说明符。
C ++中的公共,受保护和私有继承
public
、protected
和 private
继承具有以下特点:
public
使基类的public
成员在派生类中仍是public
,基类的protected
成员派生类中仍是protected
。protected
使基类的public
和protected
成员成为派生类中的protected
成员。private
使基类的public
和protected
成员成为派生类中的private
成员。
注意: 派生类无法访问基类的 private
成员。
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class PublicDerived: public Base {
// x is public
// y is protected
// z is not accessible from PublicDerived
};
class ProtectedDerived: protected Base {
// x is protected
// y is protected
// z is not accessible from ProtectedDerived
};
class PrivateDerived: private Base {
// x is private
// y is private
// z is not accessible from PrivateDerived
}
示例 1:C++ public 继承
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
输出
Private = 1
Protected = 2
Public = 3
在这里,我们得到的 PublicDerived
使用 public
模式继承自 Base
。
结果,在 PublicDerived
:
protected
被继承为protected
。- 酒馆并
getPVT()
作为public
继承。 pvt
不可访问,因为它在Base
是私人。
由于私有成员和受保护成员不能从 main()
访问,我们需要创建公共函数 getPVT()
和 getProt()
访问它们:
// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;
// Error: member "Base::prot" is inaccessible
cout << "Protected = " << object1.prot;
请注意,该getPVT()
函数已在 Base
内部定义。但是 getProt()
函数则是在 PublicDerived
里面定义的。
这是因为 pvt
是 Base
私有的 ,不能被 PublicDerived
访问。
然而, 由于公共继承, PublicDerived
可以访问 prot
。因此, getProt()
可以从 PublicDerived
内部访问受保护的变量。
公共继承中的可访问性
private |
protected |
public |
|
---|---|---|---|
基类 | 是的 | 是的 | 是的 |
派生类 | 不 | 是的 | 是的 |
示例 2:C++ 受保护的继承
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
输出
Private cannot be accessed.
Protected = 2
Public = 3
在这里,我们的 ProtectedDerived
来自通过 protected
模式继承自 Base
。
在 ProtectedDerived
中 :
prot
,pub
和getPVT()
作为protected
继承。pvt
不可访问,因为它在Base
是private
的。
众所周知,受保护的成员不能从类外部直接访问。因此,我们不能从 ProtectedDerived
对象使用 prot
。
这也是为什么我们需要在 ProtectedDerived
中创建 getPub()
函数,以便访问 pub
变量。
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
受保护继承中的可访问性
private |
protected |
public |
|
---|---|---|---|
基类 | 是的 | 是的 | 是的 |
派生类 | 不 | 是的 | 是(继承为受保护的变量) |
示例 3:C++ 私有继承
// C++ program to demonstrate the working of private inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PrivateDerived : private Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access private member
int getPub() {
return pub;
}
};
int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
输出
Private cannot be accessed.
Protected = 2
Public = 3
在这里,我们的 PrivateDerived
来自通过 private
模式继承自 Base
。
在 PrivateDerived
中:
prot
,pub
和getPVT()
作为private
继承。pvt
不可访问,因为它在Base
中是private
的。
众所周知,私有成员不能从类外部直接访问。因此,我们不能用 PrivateDerived
对象直接调用 getPVT()
。
这也是为什么我们需要在 PrivateDerived
创建 getPub()
函数以便访问 pub
变量。
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
私有继承中的可访问性
private |
protected |
public |
|
---|---|---|---|
基类 | 是的 | 是的 | 是的 |
派生类 | 不 | 是(继承为私有变量) | 是(继承为私有变量) |