[問題] 類別、建構子問題
程式新手
前幾天寫了一個練習題拿去問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,
1年前
, 1F
09/18 17:29, 1F
→
09/18 17:52,
1年前
, 2F
09/18 17:52, 2F
→
09/18 17:52,
1年前
, 3F
09/18 17:52, 3F
→
09/18 19:49,
1年前
, 4F
09/18 19:49, 4F
→
09/18 19:54,
1年前
, 5F
09/18 19:54, 5F
→
09/18 19:54,
1年前
, 6F
09/18 19:54, 6F
推
09/18 20:18,
1年前
, 7F
09/18 20:18, 7F
→
09/18 20:19,
1年前
, 8F
09/18 20:19, 8F
→
09/18 20:20,
1年前
, 9F
09/18 20:20, 9F
→
09/18 20:20,
1年前
, 10F
09/18 20:20, 10F
→
09/18 20:30,
1年前
, 11F
09/18 20:30, 11F
→
09/18 20:31,
1年前
, 12F
09/18 20:31, 12F
→
09/18 20:31,
1年前
, 13F
09/18 20:31, 13F
→
09/18 20:43,
1年前
, 14F
09/18 20:43, 14F
→
09/18 20:43,
1年前
, 15F
09/18 20:43, 15F
→
09/18 20:43,
1年前
, 16F
09/18 20:43, 16F
→
09/18 20:43,
1年前
, 17F
09/18 20:43, 17F
→
09/18 20:51,
1年前
, 18F
09/18 20:51, 18F
→
09/18 20:51,
1年前
, 19F
09/18 20:51, 19F
→
09/18 21:30,
1年前
, 20F
09/18 21:30, 20F
→
09/18 21:30,
1年前
, 21F
09/18 21:30, 21F
→
09/18 21:30,
1年前
, 22F
09/18 21:30, 22F
→
09/18 21:33,
1年前
, 23F
09/18 21:33, 23F
→
09/18 21:33,
1年前
, 24F
09/18 21:33, 24F
→
09/18 21:34,
1年前
, 25F
09/18 21:34, 25F
→
09/18 21:35,
1年前
, 26F
09/18 21:35, 26F
→
09/18 21:35,
1年前
, 27F
09/18 21:35, 27F
→
09/18 21:36,
1年前
, 28F
09/18 21:36, 28F
→
09/18 21:36,
1年前
, 29F
09/18 21:36, 29F
→
09/18 21:37,
1年前
, 30F
09/18 21:37, 30F
→
09/18 21:37,
1年前
, 31F
09/18 21:37, 31F
→
09/18 21:38,
1年前
, 32F
09/18 21:38, 32F
→
09/18 21:38,
1年前
, 33F
09/18 21:38, 33F
→
09/18 21:43,
1年前
, 34F
09/18 21:43, 34F
推
09/18 22:00,
1年前
, 35F
09/18 22:00, 35F
→
09/18 22:00,
1年前
, 36F
09/18 22:00, 36F
→
09/18 22:01,
1年前
, 37F
09/18 22:01, 37F
→
09/18 22:02,
1年前
, 38F
09/18 22:02, 38F
→
09/18 22:02,
1年前
, 39F
09/18 22:02, 39F
→
09/18 22:03,
1年前
, 40F
09/18 22:03, 40F
→
09/18 22:04,
1年前
, 41F
09/18 22:04, 41F
→
09/18 22:13,
1年前
, 42F
09/18 22:13, 42F
→
09/18 22:13,
1年前
, 43F
09/18 22:13, 43F
→
09/18 22:14,
1年前
, 44F
09/18 22:14, 44F
→
09/18 22:15,
1年前
, 45F
09/18 22:15, 45F
→
09/19 18:07,
1年前
, 46F
09/19 18:07, 46F
→
09/19 18:14,
1年前
, 47F
09/19 18:14, 47F
→
09/19 18:14,
1年前
, 48F
09/19 18:14, 48F
→
09/19 18:14,
1年前
, 49F
09/19 18:14, 49F
→
09/19 18:14,
1年前
, 50F
09/19 18:14, 50F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章