(继承)Problem B: 一帮学生

Problem B: 一帮学生

Description

学生Student类是Person类的子类,而且每个人都有生日,生日是Date类的对象。所以,需要定义如下类:

  1. Date类:拥有年、月、日三个int类型的属性。

  2. Person类:有一个Date类型对象的属性(表示生日)、string类型属性(表示名字),以及一个int类型的静态属性numOfPersons(对象个数)。

  3. Student类:是Person类的子类,并拥有一个int类型属性(表明学生学号),一个int类型的静态属性numOfStudents(对象个数)。
    定义上述类的构造、析构函数,并根据样例输出格式输出相应的信息。

Input

第一行整数N>0表示之后有N行输入。

之后的N行,每行包括4个整数、1个字符串,分别表示年、月、日、学号和姓名。

Output

见样例~

Sample Input

3
2010 3 4 1 Tom
2010 3 5 2 Jack
2010 3 6 3 Mary

Sample Output

Date 2010-3-4 is created.
The 1th person Tom whose birthday is 2010-3-4 is created.
The 1th student Tom whose birthday is 2010-3-4 and id is 1 is created.
Date 2010-3-5 is created.
The 2th person Jack whose birthday is 2010-3-5 is created.
The 2th student Jack whose birthday is 2010-3-5 and id is 2 is created.
Date 2010-3-6 is created.
The 3th person Mary whose birthday is 2010-3-6 is created.
The 3th student Mary whose birthday is 2010-3-6 and id is 3 is created.
Student Tom whose birthday is 2010-3-4 and id is 1 is erased.
Person Tom whose birthday is 2010-3-4 is erased.
Date 2010-3-4 is erased.
Student Jack whose birthday is 2010-3-5 and id is 2 is erased.
Person Jack whose birthday is 2010-3-5 is erased.
Date 2010-3-5 is erased.
Student Mary whose birthday is 2010-3-6 and id is 3 is erased.
Person Mary whose birthday is 2010-3-6 is erased.
Date 2010-3-6 is erased.

Append Code

int main()
{
    int year, month, day, id, i;
    string name;
    int num;
    cin>>num;
    Student **students = new Student*[num];
    for (i = 0; i < num; i++)
    {
        cin>>year>>month>>day>>id>>name;
        students[i] = new Student(year, month, day, name, id);
    }
    for (i = 0;i <num; i++)
        delete students[i];
    delete[] students;
    return 0;
}

#include <iostream>
#include <string>
using namespace std;

class Date
{
public:
    int year_(){return year;}
    int month_(){return month;}
    int day_(){return day;}
public:
    Date(int y,int m,int d):year(y),month(m),day(d){cout<<"Date "<<year_()<<"-"<<month_()<<"-"<<day_()<<" is created."<<endl;}
    ~Date(){cout<<"Date "<<year_()<<"-"<<month_()<<"-"<<day_()<<" is erased."<<endl;}
private:
    int year,month,day;
};

class Person
{
//public:
//    Date birth;
public:
    Person(int y,int m,int d,string n):birth(y,m,d),name(n){
        numOfPerson++;
        cout<<"The "<<numOfPerson<<"th person "<<name<<" whose birthday is "<<birth.year_()<<"-"<<birth.month_()<<"-"<<birth.day_()<<" is created."<<endl;}
    ~Person(){cout<<"Person "<<name<<" whose birthday is "<<birth.year_()<<"-"<<birth.month_()<<"-"<<birth.day_()<<" is erased."<<endl;}
public:
    string name_(){return name;}
    Date &birth_(){return birth;}//必须使用引用,原因可以进行样例测试可知
private:
    string name;
    Date birth;
    static int numOfPerson;
};

int Person::numOfPerson = 0;

class Student:public Person
{
public:
    Student(int y,int m,int d,string n,int id):Person(y,m,d,n)
    {
        stu=id;
    numOfStudent++;
    cout<<"The "<<numOfStudent<<"th student "<<name_()<<" whose birthday is "<<birth_().year_()<<"-"<<birth_().month_()<<"-"<<birth_().day_()<<" and id is "<<stu<<" is created."<<endl;
    }
    ~Student(){
    cout<<"Student "<<name_()<<" whose birthday is "<<birth_().year_()<<"-"<<birth_().month_()<<"-"<<birth_().day_()<<" and id is "<<stu<<" is erased."<<endl;
    }
private:
    int stu;
    static int numOfStudent;
};

int Student::numOfStudent = 0;

int main()
{
    int year, month, day, id, i;
    string name;
    int num;
    cin>>num;
    Student **students = new Student*[num];
    for (i = 0; i < num; i++)
    {
        cin>>year>>month>>day>>id>>name;
        students[i] = new Student(year, month, day, name, id);
    }
    for (i = 0;i <num; i++)
        delete students[i];
    delete[] students;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值