第5个题目(可以输入中文):
#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
int age;
string name;
int sex; //0表示男,1表示女,输入时为也为整数0/1
public:
Person()
{ age=0;
name=" ";
sex=0; //初始为男
}
void get_person(int a, string n,int s=0)
{ age=a;
name=n;
sex=s;
}
void out_person()
{ cout << "年龄:" << age <<endl;
cout << "姓名:" << name << endl;
if (sex==0)
cout << "性别:男" <<endl;
if (sex==1)
cout << "性别:女" <<endl;
}
};
class Student : public Person
{ private:
unsigned long num;
char *pmajor;
char *pschool;
public:
Student():Person()
{ num=0;
pmajor=NULL;
pschool=NULL;
}
void get_student(unsigned long nu, char *pm,char *ps)
{
num=nu;
pmajor=pm;
pschool=ps;
}
void out_student()
{ out_person();
cout << "学号:" << num <<endl;
cout << "专业:" << pmajor <<endl;
cout << "学院:" << pschool <<endl;
}
};
int main()
{ Student a_student;
int a_age;
string a_name;
int a_sex;
unsigned long a_number;
char a_pmajor[100];
char a_pschool[100];
cout << "请输入姓名:" << endl;
getline(cin,a_name);
cout << "请输入年龄:" << endl;
cin >> a_age;
cout << "请输入性别:" << endl;
cin >> a_sex;
cout << "请输入学号:" << endl;
cin >> a_number;
cout << "请输入专业:" << endl;
cin >> a_pmajor;
cout << "请输入所属学院:" << endl;
cin >> a_pschool;
a_student.get_person(a_age,a_name);
a_student.get_student(a_number,a_pmajor,a_pschool);
a_student.out_student();
return 0;
}