定义一个Person类,包含数据成员姓名、性别和出生日期,以该类为基类,派生出学生类和职工类,在学生类中增加数据成员学号、成绩,在职工类中增加数据成员职工号和工资。其中出生日期是日期类对象。

本文介绍了如何使用C++进行面向对象编程,定义了一个基础的Person类,包含姓名、性别和出生日期,并从Person类派生出Student和Teacher类。Student类增加了学号和成绩,Teacher类增加了职工号和工资。接着,通过键盘输入创建了3个学生和3个教职工的对象,并分别输出了他们的信息。最后,计算并输出了学生的平均成绩和教职工的平均工资。
摘要由CSDN通过智能技术生成

定义一个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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值