程式新手
前几天写了一个练习题拿去问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) {}
这样
但是这两个有什么不同啊@@他解释给我听但是我听不懂....
但是这两个有什么不同啊@@他解释给我听但是我听不懂....
请教板上高手!