帮下C++ 题

来源:百度知道 编辑:UC知道 时间:2024/08/23 07:24:05
设计父亲类father和母亲类mother和子女类child,其主要数据是姓名,年龄与民族,子女继承父亲的姓,母亲的民族,最后输出子女及其父母的姓名,年龄和民族信息

用虚基类吧

#include <iostream>
#include <string>

using namespace std;

class People
{
public:
virtual void display() = 0;
~People();
private:
int age; //年龄
std::string name; //姓名
};

class Father : public People
{
public:
void display(){cout << "Name: " << name << endl;}
private:
std::string surname;//姓
std::string name;
};

class Mother: public People
{
public:
void display() {cout << "Nationality: " << nationality << endl;}
private:
std::string nationality;//民族
};

//这里必要的话要用虚继承
class Child : public Father, public Mother
{
void display();
};

int main()
{
//调试
}