</pre><pre name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;
class Person
{
string name; // 姓名
int age; // 年龄
public:
Person() {}
void setname(string na)
{
name=na;
}
void setage(int a)
{
age=a;
}
string getname()
{
return name;
}
int getage()
{
return age;
}
};
class Leader: virtual public Person // (1)
{
string job; // 职务
string dep; // 部门
public:
Leader() { }
void setjob(string jb)
{
job=jb;
}
void setdep(char dp[])
{
dep=dp;
}
string getjob()
{
return job;
}
string getdep()
{
return dep;
}
};
class Engineer: virtual public Person // (2)
{
string major; // 专业
string prof; // 职称
public:
Engineer () { }
void setmajor(string maj)
{
major=maj;
}
void setprof(string pf)
{
prof=pf;
}
string getmajor()
{
return major;
}
string getprof()
{
return prof;
}
};
class chairman: public Leader,public Engineer { } ;// (3)
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;
}
C++作业5
最新推荐文章于 2024-08-12 20:03:23 发布