English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
L'ereditarietà C++ può essere singola o multipla, ogni connessione di ereditarietà può essere public, protected, private, o virtual o non virtual. Poi ci sono le opzioni dei vari membri delle funzioni che possono essere virtual, non virtual o pure virtual. Questo articolo esamina solo alcuni punti chiave.
Ereditarietà pubblica, ad esempio:
1 class base
2 {...}
3 class derived:public base
4 {...}
Se così si scrive, il compilatore capisce che l'oggetto di tipo derived è anche di tipo base, ma l'oggetto di tipo base non è di tipo derived. Questo è molto importante. Quindi, i parametri di funzione di tipo base sono adatti per derived, mentre i parametri di funzione di tipo derived non sono adatti per base. Ecco un codice di verifica, una funzione con parametro di tipo base, che accetta derived dovrebbe eseguire con successo, al contrario, una funzione con parametro derived
#include <iostream> #include <stdio.h> class base { public: base() :baseName(""),baseData(0) {} base(std::string bn,int bd) :baseName(bn),baseData(bd) {} std::string getBaseName() const { return baseName; } int getBaseData()const { return baseData; } private: std::string baseName; int baseData; }; class derived:public base { public: derived():base(),derivedName("") {} derived(std::string bn,int bd,std::string dn) :base(bn,bd),derivedName(dn) {} std::string getDerivedName() const { return derivedName; } private: std::string derivedName; }; void show(std::string& info,const base& b) { info.append("Name is "); info.append(b.getBaseName()); info.append(", baseData is "); char buffer[10]; sprintf(buffer,"%d",b.getBaseData()); info.append(buffer); } int main(int argc, char* argv[]) { base b("test",10); std::string s; show(s,b); std::cout<<s<<std::endl; derived d("btest",5,"dtest"); std::string ss; show(ss,d); std::cout<<ss<<std::endl; return 0; }
Running result is:
base:baseName is test, baseData is 10
base:baseName is btest, baseData is 5
Modify the code below to change the function parameter to derived
void show2(std::string& info,const derived& d) { info.append("Name is "); info.append(d.getBaseName()); info.append(", baseData is "); char buffer[10]; sprintf(buffer,"%d",d.getBaseData()); info.append(buffer); }
Call show(ss,d); Compiler error reported
1 derived_class.cpp: In function `int main(int, char**)': 2 derived_class.cpp:84: error: invalid initialization of reference of type 'const derived&' from expression of type 'base' 3 derived_class.cpp:70: error: in passing argument 2 of `void show2(std::string&, const derived&)'
The second point verifies various forms of inheritance, first giving a table
Inheritance method \ member type | public | protected | private |
public | public | protected | Cannot inherit |
protected | protected | protected | Cannot inherit |
private | private | private | Cannot inherit |
Here is an explanation, it simply expresses the members of the base class, which, after being inherited by public, protected, and private, become the types shown in the table in the derived class
class base { public: std::string testPublic() { return std::string("this is public base"); } protected: std::string testProtected() { return std::string("this is protected base"); } private: std::string testPrivate() { return std::string("this is private base"); } }; class derivedPublic:public base { public: std::string testPubPublic() { return testPublic() += "in derived"; } std::string testProPublic() { return testProtected() += "in derived"; } std::string testPriPublic() { return testPrivate() += "in derived"; } }; int main(int argc, char* argv[]) { derivedPublic dpub; std::cout << dpub.testPublic() << std::endl; }
The following error is reported, indicating that testPrivate() is not a private function of derived but a private function of base
derived11.cpp:16: error: `std::string base::testPrivate()' is private derived11.cpp:36: error: within this context
This verifies that private type members cannot be inherited (public, private, protected). Note: private and protected are omitted for proof.
Below, it is verified that testProtected can be inherited by the third-level derived class, but cannot be directly called by the third-level class, which indicates that the inheritance type is protected after public inheritance, and if the base class is of Public type, its members can be inherited and directly accessed.
#include <iostream> #include <string> class base { public: std::string testPublic() { return std::string("this is public base"); } protected: std::string testProtected() { return std::string("this is protected base"); } private: std::string testPrivate() { return std::string("this is private base"); } }; class derivedPublic:public base { public: std::string testPubPublic() { return testPublic() += "in derived"; } std::string testProPublic() { return testProtected() += "in derived"; } // std::string testPriPublic() // { // return testPrivate() += "in derived"; // } }; class deepDerived:public derivedPublic { public: std::string deepProtected() { return testProtected() += "in deep"; } std::string deepPublic() { return testPublic() +="indeep"; } }; int main(int argc, char* argv[]) { derivedPublic dpub; std::cout << dpub.testProtected() << std::endl; deepDerived deepdpub; std::cout<<deepdpub.testPublic() <<std::endl; std::cout<<deepdpub.testProtected() <<std::endl; std::cout<<deepdpub.deepProtected() <<std::endl; std::cout<<deepdpub.deepPublic() <<std::endl; }
Qui il server segnala un errore
derived12.cpp:13: errore: `std::string base::testProtected()' è protetta derived12.cpp:62: errore: in questo contesto
In questo modo è stata dimostrata una cosa pubblica e un'altra protetta. La protetta non può essere chiamata direttamente, ma può essere chiamata da un membro pubblico dopo l'ereditarietà.
La parte seguente è stata dimostrata, i dettagli del processo sono omessi. Se siete interessati a questa parte di verifica, potete vedere il codice qui sotto.
#include <iostream> #include <string> class base { public: std::string testPublic() { return std::string("this is public base"); } protected: std::string testProtected() { return std::string("this is protected base"); } private: std::string testPrivate() { return std::string("this is private base"); } }; class derivedPublic:public base { public: std::string testPubPublic() { return testPublic() += "in derived"; } std::string testProPublic() { return testProtected() += "in derived"; } // std::string testPriPublic() //Il membro privato non è stato ereditato // { // return testPrivate() += "in derived"; // } }; class deepDerived:public derivedPublic { public: std::string test() { return testPublic() +="in 3"; } }; class derivedProtected:protected base { public: std::string testPubProtected() { return testPublic() += "in derived"; } std::string testProProtected() { return testProtected() += "in derived"; } }; class deepDerived2:public derivedProtected { public: std::string test() { return testPublic() +="in 3"; } }; class derivedPrivate:private base { public: std::string testPubPirvate() { return testPublic() += "in derived"; } std::string testProPrivate() { return testProtected() += "in derived"; } }; //class deepDerived3:public derivedPrivate //{ // public: // std::string test() // { // return testPublic() += "in 3"; // } //}; int main(int argc, char* argv[]) { derivedPublic dpub; //derivedProtected dpro; //derivedPrivate dpri; std::cout << dpub.testPublic() << std::endl; //std::cout << dpub.testProtected() << std::endl; //L'utente che eredita non può usarle //cout << dpub.testPrivate() << std::endl; //Le funzioni di base sono private std::cout << dpub.testPubPublic() << std::endl; std::cout << dpub.testProPublic() << std::endl; //std::cout<<dpub.testPriPrivate()<<std::endl; //没有被继承 deepDerived dd; std::cout<<dd.test()<<std::endl; derivedProtected dpro; //std::cout<<dpro.testPublic()<<std::endl; //变成protected类型 std::cout<<dpro.testPubProtected()<<std::endl; std::cout<<dpro.testProProtected()<<std::endl; deepDerived2 dd2; std::cout<<dd2.test()<<std::endl; derivedPrivate dpri; std::cout<<dpri.testPubPirvate()<<std::endl; std::cout<<dpri.testProPrivate()<<std::endl; // deepDerived3 dd3; // std::cout<<dd3.test()<<std::endl; }
以上就是对C++继承的资料整理,后续将继续补充相关资料,感谢大家对本站的支持!
声明:本文内容来自网络,版权属于原作者,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未进行人工编辑处理,也不承担相关法律责任。如果您发现涉嫌版权的内容,请发送邮件至:notice#oldtoolbag.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立即删除涉嫌侵权内容。