设计一个 Studnet(学生)类
1、基本信息:学号、姓名、性别、出生日期、年级、班级、院系、专业;
其中:基本信息为 private 属性,成员函数为 public 属性;
2、Student 类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造
函数
3、基本功能:
1)使用对象数组保存学生对象;
2)可以从键盘输入学生的基本信息;
3)SetInfo(形参表)函数可以修改学生的基本信息函数,例如:班级、专业等;
4)Show()函数显示学生信息;
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
Student();//缺省构造函数
Student(int ,string ,char,int,int,int, int,int,string,string);//带参数构造函数。
Student(int ,string ,char,int,int,int);//默认为同一个年级。班级的人。
void SetInfo(int g,int c,string dep,string spe){
grade=g;
Class=c;
department=dep;
specialty=spe;
}
void Show(Student *p,int n);
friend istream& operator >>(istream&,Student&);
friend ostream& operator <<(ostream&,Student&);
private:
int num;
string name;
char sex;
int year;
int month;
int day;
int grade;
int Class;//c大写。
string department;
string specialty;
};
Student::Student(){
num=1000;
name="xiaoming" ;
sex='f';
year=2000;
month=1;
day=1;
grade=2019;
Class=3;
department="computer";
specialty="computer science";
}
Student::Student(int n ,string nam,char s,int y,int m,int d, int g,int c,string dep,string spe){
num=n;
name=nam;
sex=c;
year=y;
month=m;
day=d;
grade=g;
Class=c;
department=dep;
specialty=spe;
}
Student::Student(int n ,string nam,char s,int y,int m,int d){
num=n;
name=nam;
sex=s;
year=y;
month=m;
day=d;
grade=2019;
Class=3;
department="computer";
specialty="computer science";
}
void Student::Show(Student *p,int n){
cout<<"学生的学号、姓名、性别、出生日期、年级、班级、院系、专业为:"<<endl;
for(int i=0;i<n;i++){
cout<<p[i]<<endl;
}
}
istream& operator >>(istream&input,Student&stu){
input>>stu.num>>stu.name>>stu.sex>>stu.year>>stu.month>>stu.day;
input>>stu.grade>>stu.Class>>stu.department>>stu.specialty;
return input;
}
ostream& operator <<(ostream&output,Student&stu){
output<<stu.num<<" "<<stu.name<<" "<<stu.sex<<" "<<stu.year<<" "<<stu.month<<" "<<stu.day<<" ";
output<<stu.grade<<" "<<stu.Class<<" "<<stu.department<<" "<<stu.specialty;
return output;
}
int main(){
cout<<"请输入你想输入的学生的个数"<<endl;
int n;
cin>>n;
cout<<endl;
//创立学生数组:
Student stu[100];
for(int i=0;i<n;i++) {
cout<<"请输入第"<<i+1<<"个学生的学号、姓名、性别、出生年、月、日、年级、班级、院系、专业;" ;
cin>>stu[i] ;
cout<<endl;
}
stu[0].Show(stu,n);
//缺省构造函数的使用:
cout<<"请输入你想修改的学生的序号,并输入年级、班级、院系、专业,在结尾输入0表示结束。" ;
int x;
while(cin>>x&&x) {
int grade,Class;
string dep,spe;
cin>>grade>>Class>>dep>>spe;
stu[x-1].SetInfo(grade,Class,dep,spe);
cout<<"若您还想修改信息,请输入上面展示的学生的序号,然后输入年级、班级、院系、专业,否则输入0结束" <<endl;
}
cout<<"---------------------------------------------------" <<endl<<endl;
stu[0].Show(stu,n);
return 0;
}
运行结果: