#include <string>
#include <iostream>
using namespace std;
//定义动物类
class Animal {
public:
string m_name;
};
//定义狼类,虚继承动物类
class Wolf:virtual public Animal{
public:
int m_shape;
void PrintState01() {
cout << "姓名:" << this->m_name;
cout << ",爪子锋利度为:" << this->m_shape << endl;
}
};
//定义人类,虚继承动物类
class Human:virtual public Animal{
public:
int m_it;
void PrintState02() {
cout << "姓名:" << this->m_name;
cout << ",智力为:" << this->m_it << endl;
}
};
//定义狼人类,继承狼类和人类
class Werewolf :public Human, public Wolf{
public:
void SetName(string name) {
this->m_name = name;
}
void SetState(int shape, int intell) {
this->m_shape = shape;
this->m_it = intell;
}
void PrintAllState() {
this->PrintState01();
this->PrintState02();
}
};
若人类和狼类都定义成员变量m_name,当狼人类调用是会发生成员变量名称重复而不明确,不妨定义动物类,将m_name设置到动物类下,定义人类和动物类是虚继承动物类就可避免此问题。