定义一个Person类,包含数据成员姓名、性别和出生日期,以该类为基类,派生出学生类和职工类,在学生类中增加数据成员学号、成绩,在职工类中增加数据成员职工号和工资。其中出生日期是日期类对象。学生信息由键盘输入(3个学生),计算学生的平均成绩和职工信息由键盘输入(三个职工),计算职工的平均工资
#include <iostream>
#include <string>
using namespace std;
class DDate
{
public:
int Year;
int Month;
int Day;
DDate() {}
DDate(int Year, int Month, int Day)
{
this->Year = Year;
this->Month = Month;
this->Day = Day;
}
int getYear()
{
return Year;
}
int getMonth()
{
return Month;
}
int getDay()
{
return Day;
}
};
class person
{
protected:
string name, sex;
DDate t;
public:
person() {}
person(string name ,string sex, int Year, int Month, int Day):t(Year, Month, Day)
{
this->name = name;
this->sex = sex;
}
void output();
};
void person::output()
{
cout << "名字: " << name << endl;
cout << "性别: " << sex << endl;
cout << "出生日期:"<<t.getYear() << "年 " << t.getMonth() << "月 " << t.getDay() << "日 " << endl;
}
class student :public person
{
private:
string Xuehao;
float Chengji;
public:
student() {}
student(string name, string sex,string x, float c,int Year,int Month,int Day) : person(name, sex, Year, Month, Day)
{
Xuehao = x;
Chengji = c;
}
void output()
{
person::output();
cout << "学号: " << Xuehao << "成绩: " << Chengji << endl;
}
float get_Chengji()
{
return Chengji;
}
};
class teacher :public person
{
private:
string GongHao;
float Gongzi;
public:
teacher() {}
teacher(string name, string sex,string GH , float GZ,int Year, int Month, int Day): person(name,sex,Year,Month,Day)
{
GongHao = GH;
Gongzi = GZ;
}
void output()
{
person::output();
cout << "工号: " << GongHao << "工资: " << Gongzi << endl;
}
float get_Gongzi()
{
return Gongzi;
}
};
int main()
{
string S_Name, S_Sex, S_Num,T_Name,T_Sex,T_Num;
int S_Year,S_Month,S_Day,T_Year,T_Month,T_Day;
float S_Score,T_Salary;
student* S = new student[3];
teacher* T = new teacher[3];
for (int i=0;i<3;i++)
{
cout << "请输入第" <<i+1<<"个学生的姓名" << endl;cin >> S_Name;
cout << "请输入第" <<i+1<<"个学生的性别" << endl;cin >> S_Sex;
cout << "请输入第" <<i+1<<"个学生的出生年" << endl;cin >> S_Year;
cout << "请输入第" <<i+1<<"个学生的出生月" << endl;cin >> S_Month;
cout << "请输入第" <<i+1<<"个学生的出生日" << endl;cin >> S_Day;
cout << "请输入第" <<i+1<<"个学生的学号" << endl;cin >> S_Num;
cout << "请输入第" <<i+1<<"个学生的成绩" << endl;cin >> S_Score;
S[i] = student(S_Name, S_Sex, S_Num, S_Score, S_Year, S_Month, S_Day);
}
for (int j = 0; j < 3; j++)
{
cout << "请输入第" << j + 1 << "个教职工的姓名" << endl;cin >> T_Name;
cout << "请输入第" << j + 1 << "个教职工的性别" << endl;cin >> T_Sex;
cout << "请输入第" << j + 1 << "个教职工的出生年"<< endl;cin >> T_Year;
cout << "请输入第" << j + 1 << "个教职工的出生月"<< endl;cin >> T_Month;
cout << "请输入第" << j + 1 << "个教职工的出生日"<< endl;cin >> T_Day;
cout << "请输入第" << j + 1 << "个教职工的工号" << endl;cin >> T_Num;
cout << "请输入第" << j + 1 << "个教职工的工资" << endl;cin >> T_Salary;
T[j] = teacher(T_Name, T_Sex, T_Num, T_Salary, T_Year, T_Month, T_Day);
}
for (int k = 0; k < 3; k++)
{
S[k].output();
T[k] .output();
}
cout << "平均成绩为:" <<( S[0].get_Chengji() + S[1].get_Chengji() + S[2].get_Chengji())/3 << endl;
cout << "平均工资为:" << (T[0].get_Gongzi() + T[1].get_Gongzi() + T[2].get_Gongzi()) / 3 << endl;
}