📅  最后修改于: 2020-09-25 05:07:39             🧑  作者: Mango
在C++继承中,我们可以在不同的访问模式下从基类派生一个子类。例如,
class Base {
.... ... ....
};
class Derived : public Base {
.... ... ....
};
注意代码中的关键字public
class Derived : public Base
这意味着我们已经在公共模式下从基类创建了派生类。另外,我们还可以在受保护或私有模式下派生类。
这3个关键字( public
, protected
和private
)在C++继承中称为访问说明符 。
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
}
// C++ program to demonstrate the working of public inheritance
#include
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
:
由于私有成员和受保护成员不可访问,因此我们需要创建公共函数getPVT()
和getProt()
来访问它们:
// Error: member "Base::pvt" is inaccessible
cout << "Private = " << object1.pvt;
// Error: member "Base::prot" is inaccessible
cout << "Protected = " << object1.prot;
// C++ program to demonstrate the working of protected inheritance
#include
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
在这里,我们以保护模式从Base
派生了ProtectedDerived
。
结果,在ProtectedDerived
:
我们知道,不能直接访问受保护的成员。
其结果是,我们不能使用getPVT()
从ProtectedDerived
。这就是为什么我们需要在ProtectedDerived
中创建getPub()
函数以访问pub
变量的原因。
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;
// C++ program to demonstrate the working of private inheritance
#include
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
在这里,我们以私有模式从Base
派生PrivateDerived
。
结果,在PrivateDerived
:
众所周知,私有成员无法直接访问。
其结果是,我们不能使用getPVT()
从PrivateDerived
。这就是为什么我们需要在PrivateDerived
中创建getPub()
函数以访问pub
变量的原因。
// Error: member "Base::getPVT()" is inaccessible
cout << "Private = " << object1.getPVT();
// Error: member "Base::pub" is inaccessible
cout << "Public = " << object1.pub;