Tässä opetusohjelmassa opitaan käyttämään julkista, suojattua ja yksityistä perintöä C ++: ssa esimerkkien avulla.
C ++ -perinnössä voimme johtaa lapsiluokan perusluokasta eri pääsytiloissa. Esimerkiksi,
class Base (… .… … . ); class Derived : public Base (… .… … . );
Huomaa avainsana public
koodissa
class Derived : public Base
Tämä tarkoittaa, että olemme luoneet johdetun luokan perusluokasta julkisessa tilassa . Vaihtoehtoisesti voimme johtaa luokkia myös suojatuissa tai yksityisissä tiloissa.
Näiden 3 avainsanat ( public
, protected
, ja private
) tunnetaan pääsy suunnittelijat C ++ perintö.
julkinen, suojattu ja yksityinen perintö C ++: ssa
julkisella , suojatulla ja yksityisellä perinnöllä on seuraavat ominaisuudet:
- julkinen perintö saa perusluokan
public
jäsenetpublic
johdettuun luokkaan, ja perusluokanprotected
jäsenet pysyvätprotected
johdetussa luokassa. - suojattu perintö tekee perusluokan
public
japrotected
jäsenetprotected
johdetusta luokasta. - yksityinen perintö tekee perusluokan
public
japrotected
jäsenetprivate
johdetusta luokasta.
Huomaa: private
perusluokan jäsenet eivät pääse johdettuun luokkaan.
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 )
Esimerkki 1: C ++ - julkinen perintö
// 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; )
Tuotos
Yksityinen = 1 Suojattu = 2 Julkinen = 3
Täällä olemme johdettu PublicDerived
mistä Base
on julkisen tilassa .
Tämän seurauksena PublicDerived
:
- prot on peritty suojattuna .
- pubissa ja
getPVT()
ovat perittyjä julkisina . - pvt: hen ei pääse, koska se on yksityinen maassa
Base
.
Koska yksityiset ja suojatut jäsenet eivät ole käytettävissä, meidän on luotava julkisia toimintoja getPVT()
ja getProt()
pääsy niihin:
// Error: member "Base::pvt" is inaccessible cout << "Private = " << object1.pvt; // Error: member "Base::prot" is inaccessible cout << "Protected = " << object1.prot;
Esteettömyys julkisessa perinnössä
Esteettömyys | yksityiset jäsenet | suojattuja jäseniä | julkiset jäsenet |
---|---|---|---|
Perusluokka | Joo | Joo | Joo |
Johdettu luokka | Ei | Joo | Joo |
Esimerkki 2: C ++ - suojattu perintö
// 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; )
Tuotos
Yksityiseen ei pääse. Suojattu = 2 Julkinen = 3
Täällä olemme johdettu ProtectedDerived
mistä Base
on suojatussa tilassa .
Tämän seurauksena ProtectedDerived
:
- prot, pubi ja
getPVT()
periytyvät suojattuna . pvt
ei ole käytettävissä, koska se on yksityinen maassaBase
.
Kuten tiedämme, suojattuihin jäseniin ei pääse suoraan.
Tämän seurauksena emme voi käyttää getPVT()
kohteesta ProtectedDerived
. Siksi meidän on luotava getPub()
funktio ProtectedDerived
päästäksesi pubimuuttujaan.
// Error: member "Base::getPVT()" is inaccessible cout << "Private = " << object1.getPVT(); // Error: member "Base::pub" is inaccessible cout << "Public = " << object1.pub;
Esteettömyys suojatussa perinnössä
Esteettömyys | yksityiset jäsenet | suojattuja jäseniä | julkiset jäsenet |
---|---|---|---|
Perusluokka | Joo | Joo | Joo |
Johdettu luokka | Ei | Joo | Kyllä (peritty suojatuina muuttujina) |
Esimerkki 3: C ++ - yksityinen perintö
// 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; )
Tuotos
Yksityiseen ei pääse. Suojattu = 2 Julkinen = 3
Täällä olemme johdettu PrivateDerived
peräisin Base
vuonna yksityistilaan .
Tämän seurauksena PrivateDerived
:
- prot,
pub
jagetPVT()
ne peritään yksityisiksi . - pvt: hen ei pääse, koska se on yksityinen maassa
Base
.
Kuten tiedämme, yksityisiin jäseniin ei voida ottaa yhteyttä suoraan.
Tämän seurauksena emme voi käyttää getPVT()
kohteesta PrivateDerived
. Siksi meidän on luotava getPub()
funktio PrivateDerived
päästäksesi pubimuuttujaan.
// Error: member "Base::getPVT()" is inaccessible cout << "Private = " << object1.getPVT(); // Error: member "Base::pub" is inaccessible cout << "Public = " << object1.pub;
Esteettömyys yksityisessä perinnössä
Esteettömyys | yksityiset jäsenet | suojattuja jäseniä | julkiset jäsenet |
---|---|---|---|
Perusluokka | Joo | Joo | Joo |
Johdettu luokka | Ei | Kyllä (peritty yksityisinä muuttujina) | Kyllä (peritty yksityisinä muuttujina) |