设计学生类Student,成员数据包括学号(int Id)、姓名(char name[9])成绩(int score)。根据给定的main函数设计必要的成员函数。 main函数已给定,提交时只需要提交main函数外的代码部分。 #include<iostream> #include<cstring> using namespace std; //你提交的代码在这里 int main() { Student stu1; cout<<"Show object stu1:"; cout<<stu1.GetId()<<"-"<<stu1.GetName()<<"-"<<stu1.GetScore()<<endl; int no,s; char xm[9]; cin>>no>>xm>>s; Student stu2(no,xm,s); cout<<"Show object stu2:"; cout<<stu2.GetId()<<"-"<<stu2.GetName()<<"-"<<stu2.GetScore()<<endl; cin>>no>>xm>>s; cout<<"Reset and Show object stu1:"; stu1.Set(no,xm,s); stu1.Show(); cout<<"Reset and Show object stu2:"; stu2.Set(); stu2.Show(); return 0; } 输入 2行,每行3个数据。 第一个数据为学号,int类型,第2个数据为姓名,字符串类型,第3个数据为分数,int类型。 输出 按照样例输出格式输出。 输入样例 1 10001 zhangsan 98 10002 lisi 87 输出样例 1 Show object stu1:10000-NULL-0 Show object stu2:10001-zhangsan-98 Reset and Show object stu1:10002-lisi-87 Reset and Show object stu2:99999-NONAME-100
时间: 2024-03-23 12:40:24 浏览: 85
```
#include<iostream>
#include<cstring>
using namespace std;
class Student {
private:
int Id, score;
char name[9];
public:
Student(int id = 10000, const char* n = NULL, int s = 0);
int GetId() const;
const char* GetName() const;
int GetScore() const;
void Set(int id = 99999, const char* n = "NONAME", int s = 100);
void Show() const;
};
Student::Student(int id, const char* n, int s) {
Id = id;
score = s;
if (n != NULL) {
strncpy(name, n, 8);
name[8] = '\0';
}
else {
strcpy(name, "NULL");
}
}
int Student::GetId() const {
return Id;
}
int Student::GetScore() const {
return score;
}
const char* Student::GetName() const {
return name;
}
void Student::Set(int id, const char* n, int s) {
Id = id;
score = s;
if (n != NULL) {
strncpy(name, n, 8);
name[8] = '\0';
}
else {
strcpy(name, "NULL");
}
}
void Student::Show() const {
cout << Id << "-" << name << "-" << score << endl;
}
int main() {
Student stu1;
cout << "Show object stu1:";
stu1.Show();
int no, s;
char xm[9];
cin >> no >> xm >> s;
Student stu2(no, xm, s);
cout << "Show object stu2:";
stu2.Show();
cin >> no >> xm >> s;
cout << "Reset and Show object stu1:";
stu1.Set(no, xm, s);
stu1.Show();
cout << "Reset and Show object stu2:";
stu2.Set();
stu2.Show();
return 0;
}
```
阅读全文