Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 805 Solved: 289
[Submit][Status]
Description
一个简单的教务系统中的用户至少包含学生、老师两类用户。两类用户都有共同的属性姓名,教师有特有的属性职称,学生有特有的属性成绩。根据给出的主函数、输入和输出,定义三个类Person、Teacher和Student,实现基本的构造和析构功能。
Input
只有一行。第1个和第2个是字符串,表示姓名、职称,第3个是一个实数,表示成绩。
Output
见样例。
Sample Input
Tom Prof 234
Sample Output
A person without name is created.
Person Tom is created.
Teacher Tom Prof is created.
Teacher Tom is erased.
Person Tom is erased.
Person Tom is created.
Student Tom 234 is created.
Student Tom is erased.
Person Tom is erased.
A person without name is erased.
HINT
Append Code
append.cc,
[Submit][Status]
#include <iostream>
#include <string>
using namespace std;
class Person
{
static int n;
protected:
string name_;
public:
Person(){n++;cout<<"A person without name is created."<<endl;}
Person(string name):name_(name){n++;cout<<"Person "<<name_<<" is created."<<endl;}
virtual ~Person(){n--;
if(n!=0)cout<<"Person "<<name_<<" is erased."<<endl;
if(n==0)cout<<"A person without name is erased."<<endl;
}
};
int Person:: n=0;
class Teacher:virtual public Person
{
protected:
string pos_;
public:
Teacher(string name,string pos):Person(name),pos_(pos){cout<<"Teacher "<<name_<<" "<<pos_<<" is created."<<endl;}
~Teacher(){cout<<"Teacher "<<name_<<" is erased."<<endl;}
};
class Student:virtual public Person
{
protected:
double sco_;
public:
Student(string name,double sco):Person(name),sco_(sco){cout<<"Student "<<name_<<" "<<sco_<<" is created."<<endl;}
~Student(){cout<<"Student "<<name_<<" is erased."<<endl;}
};
int main(){
string name,pos;
double sco;
cin>>name>>pos>>sco;
Person person;
Person *p;
p=new Teacher(name,pos);
delete p;
Person *q;
q=new Student(name,sco);
delete q;
return 0;
}