7-4 定义抽象类Person、派生类Student和类Teacher 分数 10
设计抽象类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(表示教师),第二行为生成对象参数。
输出格式: 输出具体对象的信息。
输入样例: 以下是一组输入。
5 0 10101 Peter 20010121 Computer 87.5 1 20110014 Crystal 19900103 AI 7000 0 10102 Rose 20010715 E-Commerce 90 1 20120019 David 19781218 Prof 12000 0 10103 Semon 20000405 Computer 88 输出样例: 这里给出相应的输出。
Student [id=10101, name=Peter, bir=20010121, major=Computer, score=87.5] Teacher [id=20110014, name=Crystal, bir=19900103, title=AI, salary=7000.0] Student [id=10102, name=Rose, bir=20010715, major=E-Commerce, score=90.0]
Teacher [id=20120019, name=David, bir=19781218, title=Prof, salary=12000.0]
Student [id=10103, name=Semon, bir=20000405, major=Computer, score=88.0]
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
个人自写答案:
#include<iostream>
#include<string>
#include<stdlib.h>
#include<iomanip>
using namespace std;
class Person
{
public:
Person(unsigned int id, string name, unsigned int bir,string maj); //关于"maj":这里我不打算按题目所给出的来,实际上对于程序来说title和major是一回事,只不过是人在区分罢了
unsigned int num;
string nam;
unsigned int birthday;
string major_; //这里应该避免使用"major"作为变量名,否则g++和clang都会报错
virtual void printinfo(){
};
};
Person::Person(unsigned int id, string name, unsigned int bir, string maj) :num(id), nam(name), birthday(bir), major_(maj) {
};
class Student:public Person
{
public:
Student(unsigned int id, string name, unsigned int bir, string maj, float scre);
float score;
void printinfo();
};
Student::Student(unsigned int id, string name, unsigned int bir, string maj, float scre) :Person(id, name, bir, maj), score(scre) {
};
void Student::printinfo()
{
cout.precision(1);
cout << fixed << "Student [id=" << num << ", name=" << nam << ", bir=" << birthday << ", major=" << major_ << ", score=" << fixed << setprecision(1) << score << "]" << endl; //这里留心一下fixed和setprecision(主要是后面的代码会用,这一行的使用没什么意义)
}
class Teacher:public Person
{
public:
Teacher(unsigned int id, string name, unsigned int bir, string maj, float sala):Person(id, name, bir, maj), salary(sala) {
};
float salary;
void printinfo();
};
void Teacher::printinfo()
{
cout << "Teacher [id=" << num << ", name=" << nam << ", bir=" << birthday << ", title=" << major_ << ", salary=" << fixed << setprecision(1) << salary << "]" << endl;
}
int main()
{
int cnt,typ;
cin >> cnt;
Person** ptr = (Person**)malloc(sizeof(Person*) * cnt); //指针数组
unsigned int num;
string nam;
unsigned int birthday;
string major_;
float mixed;
for (int i = 0; i < cnt; i++) //原本想整一边输入一边输出,后面发现会出BUG,便换成先输入再输出,上面会用到指针数组也是因为这个
{
cin >> typ;
cin >> num >> nam >> birthday >> major_ >> fixed>>setprecision(1)>> mixed;
switch (typ)
{
case 0:
ptr[i] = new Student(num, nam, birthday, major_,mixed);
break;
case 1:
ptr[i] = new Teacher(num, nam, birthday, major_,mixed);
break;
default:
break;
}
}
for (int i = 0; i < cnt; i++) ptr[i]->printinfo();
return 0;
}