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

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值