[問題] 類別、建構子問題

看板C_and_CPP
作者
時間
最新
留言50則留言,5人參與討論
推噓2 ( 2048 )
AID
程式新手 前幾天寫了一個練習題拿去問chatgpt #include <iostream> using namespace std; class Animal { public: string name; Animal(string, string, int); void print1(); protected: string type; int weight; }; Animal::Animal(string n, string t, int w) { name = n; type = t; weight = w; } void Animal::print1() { cout << "name:" << name << " type:" << type << " weight:" << weight; } class Cat : public Animal { public: Cat(string, string, int, int, int); void print2(); private: int body_length; int tail_length; }; Cat::Cat(string n, string t, int w, int b, int a) : Animal(n, t, w), body_length(b), tail_length(a) {} void Cat::print2() { print1(); cout << " body length:" << body_length << " tail length:" << tail_length; } class Human : public Animal { public: Human(string, string, int, int, string); void print3(); Cat pet; protected: int height; }; Human::Human(string n, string t, int w, int h, string p) : Animal(n, t, w), height(h) { pet = Cat(p, "black", 7, 30, 20); } void Human::print3() { print1(); cout << " height:" << height << " pet:" << pet.name << endl; } int main() { Human Betty("Betty", "Asian", 46, 160, "Kitty"); Betty.print3(); Betty.pet.print2(); return 0; } 結果竟然沒辦法執行xdd 後來回覆他後他又生了一個程式給我,基本上就是把黃字的部分改成 Human::Human(string n, string t, int w, int h, string p) : Animal(n, t, w), height(h), pet(p, "black", 7, 30, 20) {} 這樣 但是這兩個有什麼不同啊@@他解釋給我聽但是我聽不懂.... 但是這兩個有什麼不同啊@@他解釋給我聽但是我聽不懂.... 請教板上高手! ---- Sent from BePTT on my OPPO CPH1943 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 180.217.31.86 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1695028575.A.715.html

50 則留言

※ 編輯: amamoimi (180.217.31.86 臺灣), 09/18/2023 17:18:36
※ 編輯: amamoimi (180.217.31.86 臺灣), 09/18/2023 17:28:30

09/18 17:29, 1F藍字部分不知道為什麼會變藍 請忽略@@

09/18 17:52, 2FCat 沒有 default constructor,所以必須在member init

09/18 17:52, 3Fialization list就初始化的樣子

09/18 19:49, 4F錯誤訊息 https://i.imgur.com/XmbhsYq.png
[問題] 類別、建構子問題

09/18 19:54, 5F嗯嗯我有去run過 不過為什麼不能在human constructor的

09/18 19:54, 6Fbrace裡初始化呀?

09/18 20:18, 7F可參考 Initialization order 的部分

09/18 20:19, 8Fhttps://tinyurl.com/36mvy7e8

09/18 20:20, 9F在第三步驟提到 data member 會在 constructor 的

09/18 20:20, 10Fbody 前被初始化

09/18 20:30, 11Fhttps://pastebin.com/pDCBK8vU 這份code有加了一個

09/18 20:31, 12F沒參數的 cat constructor 就可以通過編譯 而且從

09/18 20:31, 13F輸出可以看到 constructor 執行的順序

09/18 20:43, 14F謝謝大大

09/18 20:43, 15F我覺得我越來越不懂了...

09/18 20:43, 16F那這個code的pet用的是default constructor 還是我自己

09/18 20:43, 17F寫的建構子啊?@@

09/18 20:51, 18F寫一些std::cout的指令在你自己的constructors中讓

09/18 20:51, 19F你知道compiler呼叫誰

09/18 21:30, 20Fm大給的那個code 我想說的是 如果assign不能算初始化

09/18 21:30, 21F那pet用的應該是default constructor 但是他之後又assi

09/18 21:30, 22Fgn我設的建構子給pet 那最後pet到底是用什麼建構子..?

09/18 21:33, 23F可參考 explanation 中 effect 的第一點

09/18 21:33, 24Fhttps://tinyurl.com/5n7yu67y

09/18 21:34, 25F由於data member在constructor body前就會被初始

09/18 21:35, 26F但由於沒有提供任何初始化參數 compiler會使用無參

09/18 21:35, 27F數的constructor 在你原本的 Cat 中因爲額外定義了

09/18 21:36, 28Fconstructor 所以編譯器不會自動幫你產生 default

09/18 21:36, 29Fconstructor 可參考:https://tinyurl.com/bdh9jcb5

09/18 21:37, 30F在我後來貼的code中 明確定義無參數的constructor後

09/18 21:37, 31F在Human在初始時 會先呼叫無參數的 Cat constructor

09/18 21:38, 32F進到 body 後再呼叫有參數的 constructor

09/18 21:38, 33F可參考執行截圖:https://i.imgur.com/0ErYJcw.png
[問題] 類別、建構子問題

09/18 21:43, 34Fhttps://pastebin.com/sspJe3mv 稍微修改cout部分

09/18 22:00, 35F補充一個進 body 後技術上是有參數的 ctor 建構暫時物件

09/18 22:00, 36F再使用複製指定 (或移動指定) 運算子把暫時物件放進 pet 裡

09/18 22:01, 37F在指定過去之後這個暫時物件會被銷毁, 這裡會呼叫解構子

09/18 22:02, 38F這其實跟一個普通的 Cat 變數被指定一個 Cat(...) 的過程

09/18 22:02, 39F是一模一樣的, 這並不是初始化而是一個新物件蓋過去

09/18 22:03, 40F寫在 那行裡的才是真正對 pet 變數代表的物件呼叫建構子

09/18 22:04, 41F這個基本上就是 C++ 語法這麼規定, 你寫在那裡才是建構子

09/18 22:13, 42F樓上大大講的內容執行起來大概會是這樣

09/18 22:13, 43Fhttps://pastebin.com/FswErZhD

09/18 22:14, 44Fhttps://i.imgur.com/nXSPfhR.png
[問題] 類別、建構子問題

09/18 22:15, 45F可以觀察記憶體位置瞭解技術上是怎麼運作

09/19 18:07, 46F我看的書好像沒有講到rhs...我該換本書嗎==

09/19 18:14, 47F不過我應該懂我問的問題了...總之如果沒有在initializa

09/19 18:14, 48Ftion list裡用我的建構子初始化pet,他就會自動使用def

09/19 18:14, 49Fault constructor對吧?問題是我沒有設定無引數的建構

09/19 18:14, 50F子所以程式不能執行..