学校的构成(I)
Description
学校是老师和学生组成的。现在定义Student、Teacher和School三个类来模拟一个学校。
Student类只有一个int类型的数据成员,表示学生的年级;Teacher类只有一个double类型的数据成员,表示该教师的工资;School类是由1个Student类的对象、1个Teacher类的对象组合而成。定义它们的构造函数和析构函数,使程序能产生样例所示的输出。
Input
输入4行,第1行和第3行是2个int类型的正整数;第2行和第4行是double类型的正数。
Output
见样例。
Sample Input
3
1600.31
4
2451.34
Sample Output
A student grade 3 is created.
A teacher with salary 1600.31 is created.
A school is created.
A student grade 4 is created.
A teacher with salary 2451.34 is created.
A student grade 4 is created.
A teacher with salary 2451.34 is created.
A school is created.
A school is erased.
A teacher with salary 2451.34 is erased.
A student grade 4 is erased.
A teacher with salary 2451.34 is erased.
A student grade 4 is erased.
A school is erased.
A teacher with salary 1600.31 is erased.
A student grade 3 is erased.
题目给定程序
int main()
{
int g;
double s;
cin>>g>>s;
School sch(g, s);
cin>>g;
Student stu(g);
cin>>s;
Teacher tea(s);
School(g, s);
return 0;
}
code:
#include<iostream>
using namespace std;
class Student{
int grade;
public:
Student(int nn){
grade=nn;
cout<<"A student grade "<<grade<<" is created."<<endl;
}
~Student(){
cout<<"A student grade "<<grade<<" is erased."<<endl;
}
};
class Teacher{
double salary;
public:
Teacher(double ss){
salary=ss;
cout<<"A teacher with salary "<<salary<<" is created."<<endl;
}
~Teacher(){
cout<<"A teacher with salary "<<salary<<" is erased."<<endl;
}
};
class School{
Student stu;
Teacher teac;
public:
School(int g,double s):stu(g),teac(s){
cout<<"A school is created."<<endl;
}
~School(){
cout<<"A school is erased."<<endl;
}
};
int main()
{
int g;
double s;
cin>>g>>s;
School sch(g, s);
cin>>g;
Student stu(g);
cin>>s;
Teacher tea(s);
School(g, s);
return 0;
}