栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
#include
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="<>n;
	for(i=0;i>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;iPrint();}
		else{perr[i]->Print();}}
	
	return 0;
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/935128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号