本篇內(nèi)容主要講解“C++類繼承怎么應(yīng)用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C++類繼承怎么應(yīng)用”吧!
10多年的子長(zhǎng)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整子長(zhǎng)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“子長(zhǎng)網(wǎng)站設(shè)計(jì)”,“子長(zhǎng)網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
類的繼承會(huì)首先尋找基類,若基類未實(shí)現(xiàn),則會(huì)尋找派生類的函數(shù)
1. class繼承,函數(shù)不繼承
#includeclass Base { public: Base(){} ~Base(){} int a; void setA() { a = 1; } }; class A:public Base { public: A(){} ~A(){} void setA() { a = 2; } }; class B:public Base { public: B(){} ~B(){} void setA() { a = 3; } }; int main() { A *ax = new A(); B *bx = new B(); Base *aClass = ax; Base *bClass = bx; aClass->setA(); bClass->setA(); printf("a value of a %d\n", aClass->a); printf("a value of b %d\n", bClass->a); return 0; }
2. 函數(shù)和Class都繼承
#includeclass Base { public: Base(){} ~Base(){} int a; virtual void setA() { a = 1; } }; class A:public Base { public: A(){} ~A(){} virtual void setA() { a = 2; } }; class B:public Base { public: B(){} ~B(){} virtual void setA() { a = 3; } }; int main() { A *ax = new A(); B *bx = new B(); Base *aClass = ax; Base *bClass = bx; aClass->setA(); bClass->setA(); printf("a value of a %d\n", aClass->a); printf("a value of b %d\n", bClass->a); return 0; }
運(yùn)行結(jié)果:注意派生類中可以不寫(xiě)virtual,最好寫(xiě)上,以辨別是函數(shù)繼承
若將2中基類的函數(shù)寫(xiě)為純虛函數(shù),運(yùn)行的結(jié)果一樣,但是如果基類是純虛函數(shù),派生類必須實(shí)現(xiàn)相應(yīng)的函數(shù)。
class Base { public: Base(){} ~Base(){} int a; virtual void setA()=0; };
3. 類A中不有函數(shù),則繼承自基類
class A:public Base { public: A(){} ~A(){} };
到此,相信大家對(duì)“C++類繼承怎么應(yīng)用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!