freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

[計(jì)算機(jī)軟件及應(yīng)用]第7章繼承-在線瀏覽

2024-12-03 23:21本頁面
  

【正文】 1()......A保護(hù)顯示: i=1 j=2 B公有顯示: x=3 B執(zhí)行完畢。 C在執(zhí)行 show_A1()......A保護(hù)顯示: i=100 j=200 C在執(zhí)行 show_B()......B在執(zhí)行 show_A1()......A保護(hù)顯示: i=100 j=200 B公有顯示: x=300 B執(zhí)行完畢。 i j x Show_A1() Show_A2() Show_B() 在 A 中 私有 保護(hù) 無 保護(hù) 公有 無 在 B中 不可見 保護(hù) 私有 保護(hù) 保護(hù) 公有 在 C中 不可見 保護(hù) 不可見 保護(hù) 保護(hù) 公有 繼承方式與成員的訪問屬性 派生類的構(gòu)造函數(shù)和析構(gòu)函數(shù) ? 在派生關(guān)系中,構(gòu)造函數(shù)和析構(gòu)函數(shù)是不能繼承的,對(duì)派生類要重新定義構(gòu)造函數(shù)和析構(gòu)函數(shù) 派生類構(gòu)造函數(shù)的定義格式: 派生類名(總參數(shù)列表) : 基類名(參數(shù)列表 1),子對(duì)象名 1(參數(shù)列表 2),子對(duì)象名 2(參數(shù)列表 3), … { 派生類中新增數(shù)據(jù)成員的初始化語句 } 注意: 1)派生類構(gòu)造函數(shù)可在類中聲明,類外定義,聲明時(shí)不給出基類構(gòu)造函數(shù)名及其參數(shù)列表; 2)派生類構(gòu)造函數(shù)定義時(shí),其參數(shù)列表既有參數(shù)類型也有參數(shù)名稱,而其后的基類的參數(shù)列表只有參數(shù)名稱; ? 執(zhí)行構(gòu)造函數(shù)的順序是:派生類構(gòu)造函數(shù)先調(diào)用基類的構(gòu)造函數(shù),對(duì)基類數(shù)據(jù)成員初始化,再執(zhí)行派生類構(gòu)造函數(shù)本身,對(duì)派生類數(shù)據(jù)成員初始化。 2.派生類的析構(gòu)函數(shù) ? 派生類析構(gòu)函數(shù)沒有函數(shù)類型、沒有參數(shù),其定義及使用方法與一般類的析構(gòu)函數(shù)相同。 ? 析構(gòu)函數(shù)的調(diào)用順序與構(gòu)造函數(shù)正好相反:先執(zhí)行派生類的析構(gòu)函數(shù),然后調(diào)用子對(duì)象的析構(gòu)函數(shù),最后調(diào)用基類的析構(gòu)函數(shù),對(duì)基類進(jìn)行清理。 class Point { float x, y; public: Point(float a=0, float b=0){x=a; y=b; } void SetP(float a=0, float b=0){x=a; y=b; } void Display(){ cout″位置是: (″x′, ′y″)\\n″; } }; class Circle: public Point{ float r; public: Circle(float z=1, float x=0, float y=0):point(x,y){r=z; } void SetC(float z=1, float x=0, float y=0){r=z; SetP(x, y); } void Print() {cout″圓的 ″; Display(); cout″圓的半徑是: ″rendl; } }; void main() {Circle a(); (); (6, 8, 2); (); } 運(yùn)行結(jié)果為: 圓的位置是: (0, 0) 圓的半徑是: 圓的位置是: (8, 2) 圓的半徑是: 6 【 例 】 聲明一個(gè)派生類對(duì)象的實(shí)際操作步驟。如要建立教工類,首先建立人員基類。 class member: public people//教工類公有繼承 people類 {int m_num; //工號(hào) public: char department[10]; //部門 member(long n, char* na, char s=′m′, int mn=0, char* md=″\\0″): people(n, na, s) { m_num=mn; strcpy(department, md); }//構(gòu)造函數(shù) void m_show() {cout″教工 \\t″; p_show(); //訪問基類的公有成員 cout″教工編號(hào): m_num=″m_num″單位=″departmentendl; } }; ③ 定義對(duì)象。 member w(123456, ″王一 ″, ′m′, 789, ″計(jì)算機(jī)系 ″); 創(chuàng)建該類對(duì)象時(shí),自動(dòng)調(diào)用 member類的構(gòu)造函數(shù),再由 member類的構(gòu)造函數(shù)調(diào)用基類 people類的構(gòu)造函數(shù)。 【 例 】 設(shè)計(jì)表示學(xué)校教師、職工和學(xué)生的類及其繼承關(guān)系。//姓名,性別 long idnumber。 strcpy(name,n)。 } void p_show() { cout″人員 :身份號(hào) =″idnumber。 if (sex==′m′||sex==′M′) cout″男 ″e(cuò)ndl。 } }。//學(xué)號(hào) public: int s_class。s_class=sc。 p_show()。 }}。//工號(hào) public: char department[10]。 strcpy(department,md)。 p_show()。 }}。//崗位 public: worker(long n,char* na,char s=′m′,int mn=0,char* md=″\\0″,char* st=″\\0″): member(n,na,s,mn,md){ strcpy(station,st)。 m_show()。 }}。//執(zhí)教課程 public: teacher(long n,char* na,char s=′m′,int mn=0,char* md=″\\0″, char* tc=″\\0″): member(n,na,s,mn,md){ strcpy(course,tc)。 m_show()。 }}。 ()。 ()。 ()。 ()。 ()。//公有繼承的派生類對(duì)象直接訪問基類的基類的公有成員 coutendl。 ? 例如: class D:public A,private B,protected C { … }。 ? 多重繼承構(gòu)造函數(shù)的執(zhí)行順序與單繼承相同,即先調(diào)用所有基類的構(gòu)造函數(shù),再調(diào)用子對(duì)象的構(gòu)造函數(shù),最后執(zhí)行派生類構(gòu)造函數(shù)。 include class A{ int i; public: A(int ii=0){i=ii; } void show() { cout″A∷ show()A中 i=″iendl; } }; class B{ int i; public: B(int ii=0){ i=ii; } void show() { cout″B∷ show()B中 i=″iendl; } }; class C: public A, public B{ int i; public: C(int iA=0, int iB=0, int iC=0): A(iA), B(iB) {i=iC; } void show() {cout″C∷ show()C中 i=″iendl; } }; void main() {C c(1, 2, 3); ∷ show(); //調(diào)用對(duì)象 c的基類 A的成員函數(shù) show() ∷ show(); //調(diào)用對(duì)象 c的基類 B的成員函數(shù) show() (); //調(diào)用對(duì)象 c所屬的類 C的成員函數(shù) show() } 執(zhí)行結(jié)果為: A∷ show()A中 i=1 B∷ show()B中 i=2 C∷ show()
點(diǎn)擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號(hào)-1