在 C++ 继承中,我们可以从基类中以不同的访问模式派生子 类。例如,
class Base {
.... ... ....
};
class Derived : public Base {
.... ... ....
};
请注意代码中的 关键字 public
class Derived : public Base
这意味着我们以public 模式从基类创建了一个派生类。或者,我们也可以以protected 或 private 模式派生类。
这 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 继承
// C++ program to demonstrate the working of public 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 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
在这里,我们已在public 模式下从 Base 派生了 PublicDerived。
结果,在 PublicDerived 中
- prot 被继承为protected。
- pub 和
getPVT()被继承为public。 - pvt 不可访问,因为它在
Base中是private 的。
由于 main() 无法访问 private 和 protected 成员,我们需要创建 public 函数 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 中是private 的,对 PublicDerived 是不可访问的。
然而,prot 可被 PublicDerived 访问,这要归功于 public 继承。因此,getProt() 可以从 PublicDerived 内部访问 protected 变量。
public 继承中的可访问性
| 可访问性 | private 成员 | protected 成员 | public 成员 |
|---|---|---|---|
| 基类 | 是 | 是 | 是 |
| 派生类 | 否 | 是 | 是 |
示例 2:C++ protected 继承
// C++ program to demonstrate the working of protected 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 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
在这里,我们已在protected 模式下从 Base 派生了 ProtectedDerived。
结果,在 ProtectedDerived 中
- prot、pub 和
getPVT()被继承为protected。 pvt不可访问,因为它在Base中是private 的。
正如我们所知,protected 成员不能从类外部直接访问。因此,我们不能在 ProtectedDerived 中使用 getPVT()。
这也是为什么我们需要在 ProtectedDerived 中创建 getPub() 函数来访问 pub 变量的原因。
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
protected 继承中的可访问性
| 可访问性 | private 成员 | protected 成员 | public 成员 |
|---|---|---|---|
| 基类 | 是 | 是 | 是 |
| 派生类 | 否 | 是 | 是(作为 protected 变量继承) |
示例 3:C++ private 继承
// 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
在这里,我们已在private 模式下从 Base 派生了 PrivateDerived。
结果,在 PrivateDerived 中
- prot、
pub和getPVT()被继承为private。 - pvt 不可访问,因为它在
Base中是private 的。
正如我们所知,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 继承中的可访问性
| 可访问性 | private 成员 | protected 成员 | public 成员 |
|---|---|---|---|
| 基类 | 是 | 是 | 是 |
| 派生类 | 否 | 是(作为 private 变量继承) | 是(作为 private 变量继承) |