设计一个Student类
(1)基本信息:学号、姓名、性别、出生日期、院系、专业; 数据成员中:“日期”要声明为一个内嵌子对象
(2)Student类要包含:构造函数、内联成员函数、带默认参数的构造函数、复制构造函数
(3)成员函数基本功能有:
A)可以从键盘输入学生的基本信息;
B)定义一个函数SetInfo(形参表),可以修改学生的一些基本信息函数,例如:姓名,学号可以作修改;
C)包括成员函数void Show()显示学生信息; 【提示】 注意带默认参数值的构造函数的声明与定义; 从键盘输入基本信息,调用带参数的构造函数生成学生对象; 不同类型的信息须使用合理的变量类型,姓名、院系等可定义为字符串,使用string来定义。
#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;
}