Problem P: A改错题--学生信息的输入和输出
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 164 Solved: 91
[ Submit][ Status][ Web Board]
Description
注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交。
阅读并修改以下程序,实现学生信息的输入和输出。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
class Student
{
private:
const int maxSchoolPeriod; //学制
int no; //学号
char name[20]; //姓名
public:
/*****修改的起始行******/
Student(int Period,int no,char name[]):name(name)
{
this->maxSchoolPeriod = Period;
no = no;
}
/*****修改的终止行*****/
void output()
{
cout<<maxSchoolPeriod<<" "<<no<<" "<<name<<endl;
}
};
int main()
{
int no;
char name[20];
cin>>no>>name;
Student stu1(4,no,name); //本科生4年
cin>>no>>name;
Student stu2(3,no,name); //专科生3年
stu1.output();
stu2.output();
return 0;
}
Input
第一行: 学号 姓名
第二行: 学号 姓名
Output
第一行: 学制 学号 姓名
第二行: 学制 学号 姓名
Sample Input
101 zhangsan
102 lisi
Sample Output
4 101 zhangsan
3 102 lisi
代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
class Student
{
private:
const int maxSchoolPeriod; //学制
int no; //学号
char name[20]; //姓名
public:
/*****修改的起始行******/
Student(int Period ,int no,char *name):maxSchoolPeriod(Period)
{
strcpy(this->name,name);
this->no = no;
}
/*****修改的终止行*****/
void output()
{
cout<<maxSchoolPeriod<<" "<<no<<" "<<name<<endl;
}
};
int main()
{
int no;
char name[20];
cin>>no>>name;
Student stu1(4,no,name); //本科生4年
cin>>no>>name;
Student stu2(3,no,name); //专科生3年
stu1.output();
stu2.output();
return 0;
}