衍生類別中的建構函式

來源:互聯網
上載者:User

標籤:private   double   nbsp   hone   style   基類   const   col   span   

衍生類別不能繼承基類的建構函式,必須自己定義建構函式進行新增資料成員初始化工作,如果想同時初始化基類資料成員,必須調用基類建構函式。

例如:

 1 class stu1 2 { 3 public: 4     stu1(int i, string strn, double in) 5     { 6         ID = i; 7         name = strn; 8         income = in; 9     }10 11 private:12     int ID;13     string name;14     double income;15 };16 17 class stu2 :public stu118 {19 public:20     stu2(int i, const string str, double in, string p) :stu1(i, str, in)21     {22         phoneNo = p;23     }24 25 private:26     string phoneNo;27 };28 29 int main()30 {31     stu2 st(1, "Tom", 1000.02, "12345");32 33 }

基類也可以使用初始值列表的建構函式:衍生類別的建構函式不用改變

 1 class stu1 2 { 3 public: 4     stu1(int i, string strn, double in) :ID(i), name(strn), income(in) { } 5  6 private: 7     int ID; 8     string name; 9     double income;10 };

 

衍生類別中的建構函式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.