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;
}
相关推荐
m0_587958957 分钟前
C++中的命令模式变体
开发语言·c++·算法
~无忧花开~18 分钟前
React生命周期全解析
开发语言·前端·javascript·react.js·前端框架·react
剑心诀20 分钟前
02 数据结构(C) | 线性表——顺序表的基本操作
c语言·开发语言·数据结构
人间打气筒(Ada)36 分钟前
如何基于 Go-kit 开发 Web 应用:从接口层到业务层再到数据层
开发语言·后端·golang
2501_9249526938 分钟前
代码生成器优化策略
开发语言·c++·算法
清风徐来QCQ1 小时前
八股文(1)
java·开发语言
lsx2024061 小时前
网站主机技术
开发语言
摇滚侠1 小时前
你是一名 java 程序员,总结定义数组的方式
java·开发语言·python
xyq20241 小时前
Vue3 条件语句详解
开发语言
浩浩kids1 小时前
R•Homework
开发语言·r语言