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