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

看板C_and_CPP (C/C++)作者 (佛仔)時間7月前 (2023/09/18 17:16), 7月前編輯推噓2(2048)
留言50則, 5人參與, 7月前最新討論串1/1
程式新手 前幾天寫了一個練習題拿去問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 ※ 編輯: 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, 7月前 , 1F
藍字部分不知道為什麼會變藍 請忽略@@
09/18 17:29, 1F

09/18 17:52, 7月前 , 2F
Cat 沒有 default constructor,所以必須在member init
09/18 17:52, 2F

09/18 17:52, 7月前 , 3F
ialization list就初始化的樣子
09/18 17:52, 3F

09/18 19:49, 7月前 , 4F
09/18 19:49, 4F

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

09/18 19:54, 7月前 , 6F
brace裡初始化呀?
09/18 19:54, 6F

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

09/18 20:19, 7月前 , 8F

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

09/18 20:20, 7月前 , 10F
body 前被初始化
09/18 20:20, 10F

09/18 20:30, 7月前 , 11F
https://pastebin.com/pDCBK8vU 這份code有加了一個
09/18 20:30, 11F

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

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

09/18 20:43, 7月前 , 14F
謝謝大大
09/18 20:43, 14F

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

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

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

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

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

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

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

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

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

09/18 21:33, 7月前 , 24F

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

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

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

09/18 21:36, 7月前 , 28F
constructor 所以編譯器不會自動幫你產生 default
09/18 21:36, 28F

09/18 21:36, 7月前 , 29F
constructor 可參考:https://tinyurl.com/bdh9jcb5
09/18 21:36, 29F

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

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

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

09/18 21:38, 7月前 , 33F
可參考執行截圖:https://i.imgur.com/0ErYJcw.png
09/18 21:38, 33F

09/18 21:43, 7月前 , 34F
https://pastebin.com/sspJe3mv 稍微修改cout部分
09/18 21:43, 34F

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

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

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

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

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

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

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

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

09/18 22:13, 7月前 , 43F

09/18 22:14, 7月前 , 44F

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

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

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

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

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

09/19 18:14, 7月前 , 50F
子所以程式不能執行..
09/19 18:14, 50F
文章代碼(AID): #1b21LVSL (C_and_CPP)
文章代碼(AID): #1b21LVSL (C_and_CPP)