c语言如何创建多个cpp,c++题目:创建老鼠类和猫类,成员如下:(注意多个文件的创建:*.h、*.cpp)...

这段代码定义了两个类,Mouse(鼠标)和Cat(猫),分别代表Tom和Jerry。它们都有eat()方法来表示吃东西的行为。Cat类还额外拥有eat(Mouse mouse)和caught(Mouse mouse)方法,模拟猫捕捉和吃掉老鼠的过程。在main()函数中,创建了Tom和Jerry的对象,并展示了它们的交互行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

得分:28

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

#include

using namespace std;

class Mouse{

private:

char *name;

double weight;

public:

Mouse(char *name, double weight) {

this->name = name;

this->weight = weight;

}

char *getName() {

return name;

}

void setName(char *name) {

this->name = name;

}

double getWeight() {

return weight;

}

void setWeight(double weight) {

this->weight = weight;

}

void eat(){

cout<

}

};

class Cat{

private:

char *name;

double weight;

public:

Cat(char *name, double weight) {

this->name = name;

this->weight = weight;

}

char *getName() {

return name;

}

void setName(char *name) {

this->name = name;

}

double getWeight() {

return weight;

}

void setWeight(double weight) {

this->weight = weight;

}

void eat(){

cout<

}

void eat(Mouse mouse){

cout<

}

void caught(Mouse mouse){

cout<

}

};

int main(){

Cat cat("Tom",20);

Mouse mouse("Jerry",10);

mouse.eat();

cat.eat();

cat.caught(mouse);

cout<

cat.eat(mouse);

}

6f83fddf9cb9ff6843fffd45e1807199.gif

剑栈风樯各苦辛,别时冰雪到时春

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值