C++primer 15.5節練習

來源:互聯網
上載者:User

標籤:primer   prime   str   har   code   blog   基類   c++   private   

練習15.18

只有當衍生類別公有繼承基類時,使用者代碼才能使用衍生類別向基類的轉換:B &tb=D;

Base *p=&d1; 正確,Pub_Derv是公有繼承Base

p=&d2; 錯誤,Priv_Derv是私人繼承Base

p=&d3; 錯誤,Prot_Dery是保護繼承Base

p=&dd1; 正確,Derived_from_Public是公有繼承於Pub_Dery的

p=&dd2; 錯誤,Priv_Derv是私人繼承於Base

p=&dd3; 錯誤,Prot_Dery是保護繼承於Base

練習15.19

 1 class Base   2 {   3     char priv;   4 protected:   5     int prot;   6 public:   7     void pub() {}   8     void memfcn(Base &b) { b = *this; } //正確   9 };  10 struct Pub_Dery :public Base  11 {  12     void memfcn(Base &b) { b = *this; } //正確  13     int f() { return prot; }  14 };  15 struct Prot_Dery :protected Base  16 {  17     void memfcn(Base &b) { b = *this; } //正確  18 };  19 struct Priv_Dery:private Base  20 {  21     void memfcn(Base &b) { b = *this; } //正確  22     int f1() { return prot; }  23 };  24 struct dfpub :public Pub_Dery  25 {  26     void memfcn(Base &b) { b = *this; } //正確,公有繼承衍生類別的衍生類別可以訪問其公有與保護部分  27     int use_base() { return prot; }  28 };  29 struct dfpro:public Prot_Dery  30 {  31     void memfcn(Base &b) { b = *this; } //正確,保護繼承衍生類別的衍生類別可以訪問其保護部分  32 };  33 struct dfpri :public Priv_Dery  34 {  35     void memfcn(Base &b) { b = *this; } //錯誤,Base類型不可訪問,不允許對不可訪問的基類“base”進行轉換。原因是Priv_Dery是私人繼承Base的,所以對於dfpri來說,其Priv_Dery部分的base部分是私人的 

練習15.20

見上

練習15.21

 

C++primer 15.5節練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.