派生与继承的应用1

//定义并描述一个人员类Person,并派生出学生类和教师类,再又学生类和教师类共同派生出在职读书的教师类。。。。

#include<iostream>
using namespace std;

struct birthday{
	int year;
	int month;
	int date;
};

class Person
{
private:
	char name[20];
	char sex[10];
	char ID[20];
	birthday birth;  
public:
	Person(char *name,char *sex,char *ID, birthday birth)
	{
		strcpy(this->name,name);
		strcpy(this->sex,sex);
		strcpy(this->ID,ID);
		this->birth=birth;
	}
	void ShowInfo()
	{
		cout<<"name:"<<name<<endl;
		cout<<"sex:"<<sex<<endl;
		cout<<"ID:"<<ID<<endl;
		cout<<"birth:"<<birth.year<<"-"<<birth.month<<"-"<<birth.date<<endl;
	}
};


class Student:virtual public Person
{
private:
	char stuID[10];
	double score;
public:
	Student(char *name,char *sex,char *ID, birthday birth,char *stuID,double score)
		:Person(name,sex,ID,birth)
	{
		strcpy(this->stuID,stuID);
		this->score=score;
	}
	void ShowInfo()
	{
		Person::ShowInfo();
		cout<<"studentID:"<<stuID<<endl;
		cout<<"score:"<<score<<endl;
	}
};


class Teacher:virtual public Person
{
private:
	char pos[10];
public:
	Teacher(char *name,char *sex,char *ID, birthday birth,char *pos):Person(name,sex,ID,birth)
	{
		strcpy(this->pos,pos);
	}
	void ShowInfo()
	{
		Person::ShowInfo();
        cout<<"position:"<<pos<<endl;
	}
};


class Stu_Tech:public Student,public Teacher
{
public:
	Stu_Tech(char *name,char *sex,char *ID, birthday birth,char *stuID,double score,char *pos)
		:Person(name,sex,ID,birth),Student(name,sex,ID,birth,stuID,score),Teacher(name,sex,ID,birth,pos)
	{
	}
	
	void ShowInfo()
	{
		Person::ShowInfo();
		Student::ShowInfo();
		Teacher::ShowInfo();
	}
};


int main()
{
	birthday birth={1990,1,1};
	Person p1("zhangsan","man","420117",birth);
	p1.ShowInfo();
	cout<<endl;
	Student p2("lisi","man","420118",birth,"2012210",89.0);
	p2.ShowInfo();
	cout<<endl;
	Teacher p3("wangwu","woman","420119",birth,"jiaoshou");
	p3.ShowInfo();
	cout<<endl;
	Stu_Tech p4("maliu","woman","420120",birth,"2012211",90.0,"zhujiao");
	p4.ShowInfo();
	cout<<endl;
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值