In questo tutorial impareremo a usare l'ereditarietà pubblica, protetta e privata in C ++ con l'aiuto di esempi.
Nell'ereditarietà di C ++, possiamo derivare una classe figlia dalla classe base in diverse modalità di accesso. Per esempio,
class Base (… .… … . ); class Derived : public Base (… .… … . );
Notare la parola chiave public
nel codice
class Derived : public Base
Ciò significa che abbiamo creato una classe derivata dalla classe base in modalità pubblica . In alternativa, possiamo anche derivare classi in modalità protetta o privata .
Queste 3 parole chiave ( public
, protected
e private
) sono note come specificatori di accesso nell'ereditarietà C ++.
eredità pubblica, protetta e privata in C ++
eredità pubblica , protetta e privata hanno le seguenti caratteristiche:
- l'ereditarietà pubblica rende
public
membri della classe basepublic
nella classe derivata e iprotected
membri della classe base rimangonoprotected
nella classe derivata. - l'ereditarietà protetta rende
public
eprotected
membri della classe baseprotected
nella classe derivata. - l'ereditarietà privata rende i membri
public
eprotected
della classe baseprivate
nella classe derivata.
Nota: i private
membri della classe base non sono accessibili alla classe derivata.
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 )
Esempio 1: ereditarietà pubblica C ++
// 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; )
Produzione
Privato = 1 Protetto = 2 Pubblico = 3
Qui, abbiamo derivato PublicDerived
da Base
in modalità pubblica .
Di conseguenza, in PublicDerived
:
- prot viene ereditato come protetto .
- pub e
getPVT()
vengono ereditati come pubblici . - pvt è inaccessibile poiché è privato in
Base
.
Poiché i membri privati e protetti non sono accessibili, è necessario creare funzioni pubbliche getPVT()
e getProt()
accedervi:
// Error: member "Base::pvt" is inaccessible cout << "Private = " << object1.pvt; // Error: member "Base::prot" is inaccessible cout << "Protected = " << object1.prot;
Accessibilità nell'eredità pubblica
Accessibilità | membri privati | membri protetti | membri pubblici |
---|---|---|---|
Classe di base | sì | sì | sì |
Classe derivata | No | sì | sì |
Esempio 2: ereditarietà protetta da C ++
// 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; )
Produzione
Non è possibile accedere al privato. Protetto = 2 Pubblico = 3
Qui, siamo derivati ProtectedDerived
da Base
in modalità protetta .
Di conseguenza, in ProtectedDerived
:
- prot, pub e
getPVT()
vengono ereditati come protetti . pvt
è inaccessibile poiché è privato inBase
.
Come sappiamo, non è possibile accedere direttamente ai membri protetti .
Di conseguenza, non possiamo utilizzare getPVT()
da ProtectedDerived
. Questo è anche il motivo per cui dobbiamo creare la getPub()
funzione in ProtectedDerived
per accedere alla variabile pub.
// Error: member "Base::getPVT()" is inaccessible cout << "Private = " << object1.getPVT(); // Error: member "Base::pub" is inaccessible cout << "Public = " << object1.pub;
Accessibilità in eredità protetta
Accessibilità | membri privati | membri protetti | membri pubblici |
---|---|---|---|
Classe di base | sì | sì | sì |
Classe derivata | No | sì | Sì (ereditato come variabili protette) |
Esempio 3: ereditarietà privata C ++
// 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; )
Produzione
Non è possibile accedere al privato. Protetto = 2 Pubblico = 3
Qui, abbiamo derivato PrivateDerived
da Base
in modalità privata .
Di conseguenza, in PrivateDerived
:
- prot
pub
egetPVT()
vengono ereditati come privati . - pvt è inaccessibile poiché è privato in
Base
.
Come sappiamo, non è possibile accedere direttamente ai membri privati.
Di conseguenza, non possiamo utilizzare getPVT()
da PrivateDerived
. Questo è anche il motivo per cui dobbiamo creare la getPub()
funzione in PrivateDerived
per accedere alla variabile pub.
// Error: member "Base::getPVT()" is inaccessible cout << "Private = " << object1.getPVT(); // Error: member "Base::pub" is inaccessible cout << "Public = " << object1.pub;
Accessibilità in eredità privata
Accessibilità | membri privati | membri protetti | membri pubblici |
---|---|---|---|
Classe di base | sì | sì | sì |
Classe derivata | No | Sì (ereditato come variabili private) | Sì (ereditato come variabili private) |