C++ 公共、受保护和私有继承
C++ 公有、受保护和私有继承
在本教程中,我们将通过示例学习在 C++ 中使用公共继承、保护继承和私有继承。
在C++继承中,我们可以通过不同的访问方式从基类派生出一个子类。例如,
class Base {
.... ... ....
};
class Derived : public Base {
.... ... ....
};
注意关键字 public
在代码中
class Derived : public Base
这意味着我们在公共模式中从基类创建了一个派生类 .或者,我们也可以在 protected 中派生类 或私人 模式。
这 3 个关键字(public
, protected
, 和 private
) 被称为访问说明符 在 C++ 继承中。
C++ 中的公共、受保护和私有继承
公开 , 受保护, 和私人 继承具有以下特点:
- 公共继承 使
public
基类public
的成员 在派生类中,以及protected
基类的成员仍然是protected
在派生类中。 - 受保护的继承 使
public
和protected
基类protected
的成员 在派生类中。 - 私有继承 使
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++ 公共继承
// 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
在这里,我们导出了 PublicDerived
来自 Base
在公共模式 .
结果,在 PublicDerived
:
- prot 被继承为受保护 .
- 发布 和
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
无法访问 .
但是,prot PublicDerived
可以访问 由于公共继承。所以,getProt()
可以从 PublicDerived
中访问受保护的变量 .
公共继承中的可访问性
辅助功能 | 私人会员 | 受保护的成员 | 公共成员 |
---|---|---|---|
基类 | 是的 | 是的 | 是的 |
派生类 | 没有 | 是的 | 是的 |
示例 2:C++ 保护继承
// 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
在这里,我们导出了 ProtectedDerived
来自 Base
处于保护模式 .
结果,在 ProtectedDerived
:
- prot , 发布 和
getPVT()
被继承为受保护 . pvt
无法访问,因为它是私有的 在Base
.
众所周知,受保护 不能从班级外部直接访问成员。结果,我们不能使用 getPVT()
来自 ProtectedDerived
.
这也是为什么我们需要创建 getPub()
ProtectedDerived
中的函数 为了访问 pub 变量。
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
受保护继承中的可访问性
辅助功能 | 私人会员 | 受保护的成员 | 公共成员 |
---|---|---|---|
基类 | 是的 | 是的 | 是的 |
派生类 | 没有 | 是的 | 是(作为保护变量继承) |
示例 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
来自 Base
私人模式 .
结果,在 PrivateDerived
:
- prot ,
pub
和getPVT()
被继承为private . - pvt 无法访问,因为它是私有的 在
Base
.
众所周知,私有成员不能从类外部直接访问。结果,我们不能使用 getPVT()
来自 PrivateDerived
.
这也是为什么我们需要创建 getPub()
PrivateDerived
中的函数 为了访问 pub 变量。
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
私有继承中的可访问性
辅助功能 | 私人会员 | 受保护的成员 | 公共成员 |
---|---|---|---|
基类 | 是的 | 是的 | 是的 |
派生类 | 没有 | 是(作为私有变量继承) | 是(作为私有变量继承) |
C语言