C++ 繼續詳解及實例代碼。本站提示廣大學習愛好者:(C++ 繼續詳解及實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是C++ 繼續詳解及實例代碼正文
C++繼續可所以單一繼續或多重繼續,每個繼續銜接可所以public,protected,private也能夠是virtual或non-virtual。然後是各個成員函數選項可所以virtual或non-virtual或pure virtual。本文僅僅作出一些症結點的驗證。
public繼續,例以下:
1 class base
2 {...}
3 class derived:public base
4 {...}
假如如許寫,編譯器會懂得成類型為derived的對象同時也是類型為base的對象,但類型為base的對象不是類型為derived的對象。這點很主要。那末函數形參為base類型實用於derived,形參為derived不實用於base。上面是驗證代碼,一個參數為base的函數,傳入derived應當勝利履行,相反,一個參數為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;
}
運轉成果為:
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);編譯器報錯
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&)'
第二點對各類情勢的繼續作出驗證,起首給出表格
繼續方法\成員類型 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 << dpub.testPublic() << std::endl;
}
報上面毛病,解釋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 <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;
}
這裡辦事器報錯
derived12.cpp:13: error: `std::string base::testProtected()' is protected derived12.cpp:62: error: within this context
如許就驗證了一個是public,一個是protected,protected是不克不及直接挪用的,然則被繼續後是可以被public成員挪用的。
上面的曾經證實,具體步調就略去假如對該部門驗證感興致,可以看上面代碼。
#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 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; //用戶被繼續也是沒法應用
//cout<<dpub.testPrivate()<<std::endl; //基類都是公有函數
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++ j繼續的材料整頓,後續持續彌補相干材料,感謝年夜家對本站的支撐!