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;
}
相关推荐
IsSh9nj6q35 分钟前
Python全栈应用搭建神器magic-dash .新版本介绍
开发语言·python·dash
啦啦啦啦啦zzzz1 小时前
设计模式:建造者模式
c++·设计模式·建造者模式
独隅1 小时前
CLion 在 Linux 上的完整安装与配置使用指南
linux·运维·服务器·c语言·c++·ide
Cachel wood1 小时前
hands-on-modern-rl:动手学强化学习策略梯度reinforce
开发语言·python
wuminyu1 小时前
JUC组件逐层剥离与深度剖析
java·linux·c语言·jvm·c++·算法
蓝斯4971 小时前
一碰即传,重构跨设备文件分享体验
开发语言·python·重构
宁风NF1 小时前
JavaScript:内存、垃圾回收、性能优化
开发语言·前端·javascript·学习·性能优化·es6
ShuiShenHuoLe2 小时前
Go html/template 使用入门
开发语言·golang·html
geovindu2 小时前
java: Gale-Shapley Algorithm
java·开发语言·后端·算法