问题描述:
请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:
张三,42 岁,担任技术处处长
高级工程师,从事轮机设计专业。
#include <iostream> #include <cstring> using namespace std; class Person { char* name; // 姓名 int age; // 年龄 public: Person() {} void setname(char na[]) { name=new _________________; // ( 1) strcpy(name,na); } void setage(int a) { age=a; } char* getname() { return name; } int getage() { return age; } }; class Leader: _________________ Person // ( 2) { char *job; // 职务 char *dep; // 部门 public: Leader() { } void setjob(char jb[]) { job=new _________________; // ( 3) strcpy (job, jb); } void setdep(char dp[]) { dep=new _________________ ; // ( 4) strcpy (dep, dp); } char *getjob() { return job; } char *getdep() { return dep; } }; class Engineer: _________________ Person // ( 5) { char *major; // 专业 char *prof; // 职称 public: Engineer () { } void setmajor(char maj []) { major=new _________________; // ( 6) strcpy (major,maj); } void setprof(char pf[]) { prof=new _________________; // ( 7) strcpy (prof, pf); } char*getmajor() { return major; } char*getprof() { return prof; } }; class chairman: _________________ { } ; // ( 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; }
代码:
#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: public Leader,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; }
运行结果:
时间: 2024-10-13 05:57:54