程式新手
前幾天寫了一個練習題拿去問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) {}
這樣
但是這兩個有什麼不同啊@@他解釋給我聽但是我聽不懂....
但是這兩個有什麼不同啊@@他解釋給我聽但是我聽不懂....
請教板上高手!