Problem B: 我们来做个Student类吧!
Problem B: 我们来做个Student类吧!
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1929 Solved: 1035
[Submit][Status][Web Board]
Description
请定义一个Student类,有4个属性:
1.char *name:姓名。
2.int numOfScores:课程数量
3.int *scores:所有课程的成绩。
4.int id:学生的编号。
只有3个方法:
1. 构造函数
2.析构函数
3.void Student::showStudent()方法:用于输出学生的信息。
请根据样例输出,写出该类的实现。
Input
输入分为多行。
第一行包含3个正整数M,N和P:其中M表明之后输入的测试用例数量;N表示每个人姓名的最大长度;P表示学生学习的课程的数量。
之后有M行,包含一个学生姓名(没有任何空白符)、P门课程的成绩。
Output
见样例。
注意:所有的输出两两之间用一个空格隔开,且每行输出的首尾都没有空格。
Sample Input
3 10 5 Tom 60 61 72 56 89 Jack 99 100 98 89 77 Mary 88 88 88 88 88
Sample Output
A student whose name is "Tom" and id is 1 is created! This student is "Tom" whose id is 1. This student's scores are: 60 61 72 56 89 A student whose name is "Tom" and id is 1 is erased! A student whose name is "Jack" and id is 2 is created! This student is "Jack" whose id is 2. This student's scores are: 99 100 98 89 77 A student whose name is "Jack" and id is 2 is erased! A student whose name is "Mary" and id is 3 is created! This student is "Mary" whose id is 3. This student's scores are: 88 88 88 88 88 A student whose name is "Mary" and id is 3 is erased!
HINT
Append Code
int main()
{
int cases;
char *str;
int maxLenOfString, numOfCourses;
int *scores;
cin>>cases>>maxLenOfString>>numOfCourses;
str = new char[maxLenOfString + 1];
scores = new int[numOfCourses];
for (int i = 0; i < cases; i++)
{
cin>>str;
for (int j = 0; j < numOfCourses; j++)
cin>>scores[j];
Student stu(str,scores,numOfCourses);
stu.showStudent();
}
return 0;
}
#include <iostream>
using namespace std;
class Student
{
public :
string name;
int numOfScores;
int *scores;
static int id;
Student(string arr, int *num, int sum):name(arr),numOfScores(sum)
{
id++;
scores= new int[sum];
for(int i=0; i<sum; i++)
scores[i]=num[i];
cout<<"A student whose name is "<<name<<" and id is "<<id<<" is created!"<<endl;
}
void showStudent()
{
cout<<"This student is \""<<name<<"\" whose id is "<<id<<"."<<endl;
cout<<"This student's scores are: ";
for(int i=0; scores[i]!='\0'; i++)
cout<<" "<<scores[i];
cout<<endl;
}
~Student()
{
cout<<"A student whose name is "<<name<<" and id is "<<id<<" is erased!"<<endl;
}
};
int Student::id=0;
int main()
{
int cases;
char *str;
int maxLenOfString, numOfCourses;
int *scores;
cin>>cases>>maxLenOfString>>numOfCourses;
str = new char[maxLenOfString + 1];
scores = new int[numOfCourses];
for (int i = 0; i < cases; i++)
{
cin>>str;
for (int j = 0; j < numOfCourses; j++)
cin>>scores[j];
Student stu(str,scores,numOfCourses);
stu.showStudent();
}
return 0;
}
作者:7oDo
仅供参考,请勿抄袭。
Hang Hang Hang !!!

浙公网安备 33010602011771号