Problem C: Person类与Student类的关系

Problem C: Person类与Student类的关系

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 2888   Solved: 2199
[ Submit][ Status][ Web Board]

Description

当然,一个student首先是一个person。所以,Student类是Person类的派生类。请定义Person类,包括:

1. 数据成员string name和int age,分别表示姓名和年龄。

2. 构造函数和析构函数,它们有相应的输出,见样例。

3. void show()函数:按照样例输出该对象的name和age属性值。

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

1. 数据成员int grade,表示学生所在年级。

2. 构造函数和析构函数,它们有相应的输出,见样例。

3. void show()函数:按照样例输出该对象的grade属性值。

Input

只有1行,分为三部分:一个不含空白符的字符串以及两个整数。

Output

见样例。

Sample Input

Tom 19 3

Sample Output

A person Tom whose age is 19 is created.A student whose grade is 3 is created.Name is Tom and age is 19.Grade is 3.A student whose grade is 3 is erased.A person Tom whose age is 19 is erased.

HINT

Append Code

[ Submit][ Status][ Web Board]
#include <iostream> 
#include <algorithm> 
#include <cstring> 
   
using namespace std; 
class Person 
public
     string name; 
     int age; 
     Person(string name, int age):name(name),age(age){cout<< "A person " <<name<< " whose age is " <<age<< " is created." <<endl;} 
     ~Person(){cout<< "A person " <<name<< " whose age is " <<age<< " is erased." <<endl;} 
     void show() 
    
         cout<< "Name is " <<name<< " and age is " <<age<< "." <<endl; 
    
   
}; 
class Student : public Person 
public
     int grade; 
     Student(string name, int age, int _grade):Person(name,age) 
    
         grade = _grade; 
         cout<< "A student whose grade is " <<grade<< " is created." <<endl; 
    
     void show() 
    
         cout<< "Grade is " <<grade<< "." <<endl; 
    
     ~Student(){cout<< "A student whose grade is " <<grade<< " is erased." <<endl; } 
};
int main()
{
     string n;
     int a, g;
     cin>>n>>a>>g;
     Student student(n, a, g);
     student.Person::show();
     student.show();
     return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值