#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
/**
* 定义人类: Person
*/
class Person
{
public:
Person(string name) :m_strName(name)
{
cout << "Person" << endl;
}
~Person()
{
cout << "~Person" << endl;
}
void eat()
{
cout << "eat" << endl;
}
protected:
string m_strName;
};
class Worker : virtual public Person
{
public:
Worker(string name):Person(name)
{
cout << "Worker" << endl;
}
~Worker()
{
cout << "~Worker" << endl;
}
void work()
{
cout << "work" << endl;
}
};
class Children : virtual public Person
{
public:
Children(int age,string name):m_iAge(age),Person(name)
{
cout << "Children" << endl;
}
~Children()
{
cout << "~Children" << endl;
}
void play()
{
cout << m_iAge << endl;
cout << "play" << endl;
}
protected:
int m_iAge;
};
class ChildLabourer :public Worker,public Children
{
public:
ChildLabourer(string name, int age) :Worker(name),Children(age,name)
{
cout << "ChildLabourer" << endl;
}
~ChildLabourer()
{
cout << "~ChildLabourer" << endl;
}
};
int main(void)
{
// 用new关键字实例化童工类对象
ChildLabourer *p = new ChildLabourer("jack",12);
// 调用童工类对象各方法。
p->eat();
p->work();
p->play();
delete p;
p = NULL;
system("pause");
return 0;
}
snowmanJS
onemoo
天将明96
相关分类