PTA---C++实现,定义抽象类Person、派生类Student和类Teacher

问题描述:设计抽象类Person,派生出具体类:学生类Student和教师类Teacher,创建若干不同类对象后并在主方法中测试。
数据成员定义:
Person [ID,姓名,生日]
Student [专业,成绩]
Teacher [职称,工资]
带参构造方法分别为:
Person(int id,String name, int bir)
Student(int id,String name, int bir, String major,double score)
Teacher(int id,String name, int bir, String title, double salary)
toString方法(Eclipse自动生成)

输入格式:第一行整数n表示有n个对象,每个对象占2行,第一行为数字0(表示学生)或1(表示教师),第二行为生成对象的参数。

输出格式:按行输出具体对象的信息。

#include<iostream>
#include<iomanip>
using namespace std;
class Person{
	public:
		int ID; string Name; int Birthday;
		void Set(int id, string name, int birthday){ID=id;Name=name;Birthday=birthday;}
		void Print();		
};



class Student : public Person{
	public:
		string Major; double Score;
		void Set(int id, string name, int birthday, string major, double score){Person::Set(id,name,birthday);Major=major;Score=score;}
		void Print(){cout<<"Student [";Person::Print();cout<<"major="<<Major<<", score="<<fixed<<setprecision(1)<<Score<<"]"<<endl;}
};

Student *per[10];

class Teacher : public Person{
	public:
		string Title; double Salary;
		void Set(int id, string name, int birthday, string title, double salary){Person::Set(id,name,birthday);Title=title;Salary=salary;}
		void Print(){cout<<"Teacher [";Person::Print();cout<<"title="<<Title<<", salary="<<fixed<<setprecision(1)<<Salary<<"]"<<endl;}
};

Teacher *perr[10];

void Person::Print(){cout<<"id="<<ID<<", name="<<Name<<", bir="<<Birthday<<", ";}

int main(){
	Student *stu; Teacher *tea;
	int n, i, choice;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>choice;
		if(choice==0){
			stu=new Student();
			cin>>stu->ID>>stu->Name>>stu->Birthday>>stu->Major>>stu->Score;
			per[i]=stu;}
		if(choice==1){
			tea=new Teacher();
			cin>>tea->ID>>tea->Name>>tea->Birthday>>tea->Title>>tea->Salary;
			perr[i]=tea;}
		}
	
	for(i=0;i<n;i++){
		if(per[i]){per[i]->Print();}
		else{perr[i]->Print();}}
	
	return 0;
}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值