#include <iostream>
#include <cstring>
using namespace std;
class Person
{
char* name; // 姓名
int age; // 年龄
public:
Person() {}
void setname(char na[])
{
name=new char[strlen(na)+1]; // ( 1)
strcpy(name,na);
}
void setage(int a)
{
age=a;
}
char* getname()
{
return name;
}
int getage()
{
return age;
}
};
class Leader: virtual public Person // ( 2)
{
char *job; // 职务
char *dep; // 部门
public:
Leader() { }
void setjob(char jb[])
{
job=new char[strlen(jb)+1]; // ( 3)
strcpy (job, jb);
}
void setdep(char dp[])
{
dep=new char[strlen(dp)+1] ; // ( 4)
strcpy (dep, dp);
}
char *getjob()
{
return job;
}
char *getdep()
{
return dep;
}
};
class Engineer:virtual public Person // ( 5)
{
char *major; // 专业
char *prof; // 职称
public:
Engineer () { }
void setmajor(char maj [])
{
major=new char[strlen(maj)+1]; // ( 6)
strcpy (major,maj);
}
void setprof(char pf[])
{
prof=new char[strlen(pf)+1]; // ( 7)
strcpy (prof, pf);
}
char*getmajor()
{
return major;
}
char*getprof()
{
return prof;
}
};
class chairman:virtual public Leader,virtual public Engineer { } ; // ( 8)
int main()
{
chairman c;
c.setname("张三");
c.setage(42);
c.setjob("处长");
c.setdep("技术处");
c.setmajor("轮机设计");
c.setprof("高级工程师");
cout <<c.getname() << "," <<c.getage()<<" 岁,担任" <<c.getdep() <<c.getjob() <<endl;
cout <<c.getprof() << ",从事" << c.getmajor()<< "专业" << "。 " << endl;
return 0;
}
学习心得:
十分简单,就是虚基类的应用和动态数组