读书人

人学生职员虚基类的例子

发布时间: 2012-08-26 16:48:05 作者: rapoo

人,学生,职工虚基类的例子

#include<iostream>#include<string>using namespace std;class Person{public:Person(string name1,char sex1,int age1){name=name1;sex=sex1;age=age1;}void print(){cout<<"姓名:"<<name<<endl;cout<<"性别:"<<sex<<endl;cout<<"年龄:"<<age<<endl;}protected:string name;char sex;int age;};class Student:virtual public Person{public:Student(string name1,char sex1,int age1,string mj1,char *stu_no1):  Person(name1,sex1,age1)  {  stu_no=new char[strlen(stu_no1)+1];  strcpy(stu_no,stu_no1);       mj=mj1;  }  ~Student()  {  delete []stu_no;  }  void print()  {  Person::print();  cout<<"学号:"<<stu_no<<endl;  cout<<"专业:"<<mj<<endl;  }protected:string mj;char *stu_no;};class Employee:virtual public Person{public:Employee(string name1,char sex1,int age1,string dept1,char *no1): Person(name1,sex1,age1) {    no=new char[strlen(no1)+1]; strcpy(no,no1); dept=dept1; } ~Employee() { delete []no; } void print() { Person::print(); cout<<"职工编号:"<<no<<endl; cout<<"部门:"<<dept<<endl; }protected:string dept;char *no;};class E_Student:public Employee,public Student{public:E_Student(string name1,char sex1,int age1,string dept1,string mj1,char *no1,char *stu_no1):  Person(name1,sex1,age1),Student(name1,sex1,age1,mj1,stu_no1),Employee(name1,sex1,age1,dept1,no1){}     void print() { cout<<"姓名:"<<name<<endl; cout<<"性别:"<<sex<<endl; cout<<"年龄:"<<age<<endl; cout<<"专业:"<<mj<<endl; cout<<"部门:"<<dept<<endl; cout<<"职工编号:"<<no<<endl;   }};int main(){Student stu("小明",'m',20,"数学应用","038");cout<<"大学生:"<<endl;stu.print();Employee emp("李潇潇",'f',25,"科研处","02");cout<<"职工:"<<endl;emp.print();E_Student e_s("雷明",'m',26,"教务处","j计算机科学应用","099","037489");cout<<"在职大学生:"<<endl;e_s.print();return 0;}

人,学生,职员虚基类的例子

读书人网 >编程

热点推荐