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!
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;
}
AC代码一(不推荐使用该方法)
#include <iostream>
using namespace std;
class Student
{
private:
char* name;//姓名;
int numOfScores;//课程数量
int *scores;//所有课程的成绩
int id;//学生的编号
static int sum;
public:
Student(char* name1,int* score,int n):name(name1),scores(score),numOfScores(n){++sum;id=sum;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;i<numOfScores;i++)
{
cout<<" "<<scores[i];
}
cout<<endl;
}
~Student(){cout<<"A student whose name is "<<'"'<<name<<'"'<<" and id is "<<id<<" is erased!"<<endl;}
};
int Student::sum=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;
}
1、char 类型的,比如char name,cout<<name,输出的是内容,但是,其他类型比如int* ,int* name,cout<<name输出的是地址,要想输出char类型的地址,就必须强制转化成其他类型的地址,比如void**
2、可以不用动态数组,但是说实话,以后做题的时候,最好是用动态数组,防止出现指针悬挂现象。一般动态和和静态数组可以互换使用,但是若要求输出对象创建的过程,即要需要多少个类就创建多少个类,则必须用动态
详情见:类的初体验(V)

AC代码二(推荐使用该方法)
#include <iostream>
#include <cstring>
using namespace std;
class Student
{
private:
char* name;//姓名;
int numOfScores;//课程数量
int *scores;//所有课程的成绩
int id;//学生的编号
static int sum;
public:
Student(char* name1,int* score,int n):numOfScores(n)
{
name=new char[strlen(name1)+1];
strcpy(name,name1);
scores=new int[n];
for(int i=0;i<n;i++)
scores[i]=score[i];
++sum;id=sum;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;i<numOfScores;i++)
{
cout<<" "<<scores[i];
}
cout<<endl;
}
~Student(){delete []name;delete []scores;cout<<"A student whose name is "<<'"'<<name<<'"'<<" and id is "<<id<<" is erased!"<<endl;}
};
int Student::sum=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;
}
1、int类型使用memcpy比较好,但是char使用strcpy比较好,下面是memcpy的使用方法;

2、对于引号的输出方法如下cout<<"A student whose name is "<<"\“"<<names<<"\”"<<" and id is "<<id<<" is erased!"<<endl;对于英文的"可以采用’"'的方法输出,但是对于中文的“”则需要采用"\""的方法,输出,其实不管是中文的还是英文的最好多采用这样的方法输出即"\""
本文介绍了如何在C++中定义一个Student类,包括姓名、课程数量、成绩和学生编号四个属性,以及构造函数、析构函数和显示学生信息的方法。样例展示了如何使用该类并输出学生信息。

136

被折叠的 条评论
为什么被折叠?



