C++++繼承的驗證,何為繼承?為啥C++需要繼承,我們得理解清楚先,C++繼承可以是單一繼承或多重繼承,每一個繼承連接可以是public,protected,private也可以是virtual或non-virtual,本文作出一些關鍵點的驗證,一起來了解。
public繼承,例如下:
?class base
?{...}
?class derived:public base
?{...}
如果這樣寫,編譯器會理解成類型為derived的對象同時也是類型為base的對象,但類型為base的對象不是類型為derived的對象。這點很重要。那么函數形參為base類型適用于derived,形參為derived不適用于base。下面是驗證代碼,一個參數為base的函數,傳入derived應該成功執行,相反,一個參數為derived的函數
?
?
?#include
? #include
? 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???? derived d("btest",5,"dtest");
???? std::string ss;
???? show(ss,d);
???? std::cout???? return 0;
?}
運行結果為:
?
base:baseName is test, baseData is 10
base:baseName is btest, baseData is 5
下面改改代碼,將函數參數變為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);
}
調用show(ss,d);編譯器報錯
?
?
?derived_class.cpp: In function `int main(int, char**)':
?derived_class.cpp:84: error: invalid initialization of reference of type 'const derived&' from expression of type 'base'
?derived_class.cpp:70: error: in passing argument 2 of `void show2(std::string&, const derived&)'第二點對各種形式的繼承作出驗證,首先給出表格
?
?
?
繼承方式/成員類型 | public | protected | private |
public | public | protected | 無法繼承 |
protected | protected | protected | 無法繼承 |
private | private | private | 無法繼承 |
?
這里解釋一下,這里僅僅表達基類的成員,被public,protected,private三種方式繼承后,在原基類為public,protectedc,private的成員在繼承類里類型為表格里內容
?
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 }
報下面錯誤,說明testPrivate()不是derived私有函數而是base的私有函數
derived11.cpp:16: error: `std::string base::testPrivate()' is private
derived11.cpp:36: error: within this context這樣驗證private類型成員無法被繼承(public,private,protected)注:private,protected略去不做證明
下面只要驗證 testProtected 能被第三層繼承類繼承,但是無法被第三層類直接調用就說明是public繼承后繼承類型為protected,而基類為Public類型成員則即可被繼承又可以直接調用。
?
?
?#include
?#include
?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 ??? deepDerived deepdpub;
??? std::cout??? std::cout??? std::cout??? std::cout}
這里服務器報錯
?
derived12.cpp:13: error: `std::string base::testProtected()' is protected
derived12.cpp:62: error: within this context這樣就驗證了一個是public,一個是protected,protected是不能直接調用的,但是被繼承后是可以被public成員調用的。
下面的已經證明,詳細步驟就略去如果對該部分驗證感興趣,可以看下面代碼。
?#include
?2 #include
?3 class base
?4 {
?5???? public:
?6???????? std::string testPublic()
?7???????? {
?8???????????? return std::string("this is public base");
?9???????? }
???? 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 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??? //std::cout??? //cout??? std::cout??? std::cout??? //std::cout
??? deepDerived dd;
??? std::cout
??? derivedProtected dpro;
??? //std::cout??? std::cout??? std::cout
??? deepDerived2 dd2;
??? std::cout
??? derivedPrivate dpri;
??? std::cout??? std::cout
//??? deepDerived3 dd3;
//??? std::cout}
小編帶來的這篇C++繼承的驗證,是不是特別的實用呢,更多關于C++的內容,可以多多關注下武林技術頻道。