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;
}
相关推荐
你怎么知道我是队长5 小时前
C语言---枚举变量
c语言·开发语言
李慕婉学姐5 小时前
【开题答辩过程】以《基于JAVA的校园即时配送系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·开发语言·数据库
吃茄子的猫5 小时前
quecpython中&的具体含义和使用场景
开发语言·python
云栖梦泽5 小时前
易语言中小微企业Windows桌面端IoT监控与控制
开发语言
数据大魔方5 小时前
【期货量化实战】日内动量策略:顺势而为的短线交易法(Python源码)
开发语言·数据库·python·mysql·算法·github·程序员创富
fpcc6 小时前
C++编程实践——链式调用的实践
c++
Edward.W7 小时前
Python uv:新一代Python包管理工具,彻底改变开发体验
开发语言·python·uv
小熊officer7 小时前
Python字符串
开发语言·数据库·python
月疯7 小时前
各种信号的模拟(ECG信号、质谱图、EEG信号),方便U-net训练
开发语言·python
荒诞硬汉7 小时前
JavaBean相关补充
java·开发语言