Problem C: 老师与学生

Problem C: 老师与学生

Description

定义Person类,

1.有一个int类型属性age和1个char类型属性sex,分别为年龄和姓名。

2.构造函数和析构函数,输出如样例所示的信息。

定义Student类,是Person类的子类:

1.有一个int类型属性,是学生所在的班级号。

2.构造函数与析构函数,输出如样例所示的信息。

定义Teacher类,是Person类的子类:

1.有一个int类型属性,表示老师的工资。

2.构造函数与析构函数,输出如样例所示的信息。

input

输入有多行,每行是一个测试用例。每行的第1个是一个正整数,是年龄;第2个是一个字符,表示性别;第3个是一个字符t或s,表示老师或学生;第4个是一个正整数,表示班号(对于学生)或工资(对于老师)。

Oouput

见样例

Sample Input

18 f s 1

35 m t 8001

Sample Output

Person age = 18, sex = f is created.

Student of class 1 is created.

Student of class 1 is erased.

Person age = 18, sex = f is erased.

Person age = 35, sex = m is created.

Teacher with salary 8001 is created.

Teacher with salary 8001 is erased.

Person age = 35, sex = m is erased.

HINT

Append Code

int main()
{
    Person *p;
    int age, par;
    char sex, t;
    while (cin>>age>>sex>>t>>par)
    {
        if (t == 's')
        {
            p = new Student(age, sex, par);
        }
        else
        {
            p = new Teacher(age, sex, par);
        }
        delete p;
    }
 
}

Sample Code

#include <iostream>
#include <string>
using namespace std;
 
class Person
{
protected:
   int age;
   char sex;
 
public:
    Person(int _age,char _sex):age(_age),sex(_sex){cout<<"Person age = "<<age<<", sex = "<<sex<<" is created."<<endl;}
    ~Person(){cout<<"Person age = "<<age<<", sex = "<<sex<<" is erased."<<endl;}
};
 
class Student: public Person
{
private:
    int grade;
public:
    Student(int _age,char _sex,int par):Person(_age,_sex),grade(par){cout<<"Student of class "<<grade<<" is created."<<endl;cout<<"Student of class "<<grade<<" is erased."<<endl;}
    ~Student(){}
};
class Teacher:public Person
{
private:
    int salary;
public:
    Teacher(int _age,char _sex,int a):Person(_age,_sex),salary(a){cout<<"Teacher with salary "<<salary<<" is created."<<endl;cout<<"Teacher with salary "<<salary<<" is erased."<<endl;}
    ~Teacher(){}
};
int main()
{
    Person *p;
    int age, par;
    char sex, t;
    while (cin>>age>>sex>>t>>par)
    {
        if (t == 's')
        {
            p = new Student(age, sex, par);
        }
        else
        {
            p = new Teacher(age, sex, par);
        }
        delete p;
    }
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值