C++ 狼类(Wolf)、人类(Human)、狼人类(Werewolf)

C++ 实现 定义以下三个类; 狼类(Wolf):成员变量姓名:stringName,成员变量爪子锋利度:intShape,成员函数:voidPrintStateO,按照姓名、爪子锋利度格式输出两个成员变量的值。←人类(Human):成员变量姓名:stringName,成员变量智力:intIntell,成员函数:void PrintState0)按照姓名、智力格式输出两个成员变量的值。←狼人类(Werewof),它继承狼类和人类,成员函数:voidSetName(stringname),函数用来设置两个基类的成员变量姓名。成员函数:void SetState(int shape,intintell),函数用 shape、intell两个参数分别设置狼类的爪子锋利度和人类的智力。←成员函数:void PrintAllState0,函数按照狼类,人类的顺序调用两个基类的PrintState 函数,输出他们的成员变量值。←

cpp 复制代码
#include <iostream>
#include <string>

using namespace std;

// 狼类
class Wolf {
protected:
    string name;
    int shape;

public:
    Wolf() {}

    void setName(string n) {
        name = n;
    }

    void setShape(int s) {
        shape = s;
    }

    void printState() {
        cout << "Wolf - Name: " << name << ", Shape: " << shape << endl;
    }
};

// 人类
class Human {
protected:
    string name;
    int intell;

public:
    Human() {}

    void setName(string n) {
        name = n;
    }

    void setIntell(int i) {
        intell = i;
    }

    void printState() {
        cout << "Human - Name: " << name << ", Intelligence: " << intell << endl;
    }
};

// 狼人类
class Werewolf : public Wolf, public Human {
public:
    Werewolf() {}

    void setAll(string n, int s, int i) {
        setName(n);
        setShape(s);
        setIntell(i);
    }

    void printAllState() {
        // 调用基类的printState函数
        Wolf::printState();
        Human::printState();
    }
};

int main() {
    Werewolf werewolf;
    werewolf.setAll("Werewolf", 10, 100);
    werewolf.printAllState();

    return 0;
}
相关推荐
D_evil__5 小时前
【Effective Modern C++】第三章 转向现代C++:16. 让const成员函数线程安全
c++
微风中的麦穗5 小时前
【MATLAB】MATLAB R2025a 详细下载安装图文指南:下一代科学计算与工程仿真平台
开发语言·matlab·开发工具·工程仿真·matlab r2025a·matlab r2025·科学计算与工程仿真
2601_949146535 小时前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
开源技术5 小时前
Python Pillow 优化,打开和保存速度最快提高14倍
开发语言·python·pillow
学嵌入式的小杨同学5 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
Queenie_Charlie6 小时前
前缀和的前缀和
数据结构·c++·树状数组
mftang7 小时前
Python 字符串拼接成字节详解
开发语言·python
jasligea7 小时前
构建个人智能助手
开发语言·python·自然语言处理
kokunka7 小时前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏
云栖梦泽8 小时前
易语言开发从入门到精通:补充篇·网络编程进阶+实用爬虫开发·API集成·代理IP配置·异步请求·防封禁优化
开发语言