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;
}
相关推荐
努力实干3 分钟前
Qt/C++ 模块化设计与工程组织
开发语言·c++·qt
Rabitebla8 分钟前
【Linux底层原理】 工具篇(二):Vim 编辑器基础操作与简单配置
linux·c++·编辑器·vim
阿里嘎多学长10 分钟前
2026-10-23 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Lhan.zzZ27 分钟前
Qt:动态库模块框架设计
开发语言·c++·qt
quantdash_cc32 分钟前
如何从多只股票的历史行情构建收益率矩阵?Python 与 QuantDash 实战教程
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
wuminyu34 分钟前
JVM规范学习总结-JVM规范基本概念
java·linux·c语言·jvm·c++
Demon--hx2 小时前
jsoncpp 核心概念梳理:命名空间、Value 容器与格式化输出
c++
不是万能的豆学长4 小时前
filetidy 文件整理助手:一键归类 + 规范重命名(Python 零依赖单文件工具)
大数据·开发语言·python·技术美术
懂点Tech9 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
catchadmin10 小时前
PHP 8.7 协程新提案 让跨协程代码共享请求身份
开发语言·php