实验7 类/对象/基类/派生类综合实验

本文介绍了如何设计一个名为Staff_list的类,用于存储和操作员工信息,包括姓名、性别、出生日期、入职日期、部门和工资等。文章还涉及了使用虚函数、基类和派生类的概念,以及如何创建Find函数来查找特定员工。
摘要由CSDN通过智能技术生成

编写

class Date;
class Staff;
class Teacher;
class Ad_Staff:;
class Tea_Ad_Staff;
class O_Teacher;

完成两个综合问题

6-7 定义一个Staff_List类

作者 刘利

单位 惠州学院

定义一个Staff_list类,根据main的需求设计数据成员和成员函数,完成输入员工(人数不超过100)信息的存储和输出。
class Date;
class Staff;
class Teacher;
class Ad_Staff:;
class Tea_Ad_Staff;
class O_Teacher;
以上类代码用前面1~6题自己定义的类。

思考

思考Staff_list存储员工数据时应该采用什么数据结构合适?

语法知识点

1.基类和共有派生类赋值兼容规则
2.虚函数

函数接口定义:

int main()

裁判测试程序样例:

/* 请在这里填写答案 */
int main()
{
    int n;
    //     姓名 部门 职称   职位      单位
    string name,dept,title,postation,company;
    //sex性别
    char sex,c;
    int byear,bmonth,bday,eyear,emonth,eday;    
    double salary;
    Staff_list L;
    while(cin>>n && n!=0)
    {
        cin>>name>>sex;        
        cin>>byear>>c>>bmonth>>c>>bday;
        cin>>eyear>>c>>emonth>>c>>eday;
        cin>>dept>>salary;
        // e_date入职日期 b_date 出生日期
        Date e_date(eyear,emonth,eday),b_date(byear,bmonth,bday);
        switch(n)
        {
            //添加教师 
            case 1:{                            
                cin>>title;
                Teacher t(name,sex,b_date,e_date,dept,salary,title);
                L.Add(t);                
                break;
            }
            //添加行政人员 
            case 2:{
                cin>>postation;
                Ad_Staff as(name,sex,b_date,e_date,dept,salary,postation);
                L.Add(as);                
                break;
            }
            //添加双挑人员 
            case 3:{
                cin>>postation;
                cin>>title;
                Tea_Ad_Staff tas(name,sex,b_date,e_date,dept,salary,postation,title);
                L.Add(tas);                
                break;
            }
            //添加外聘教师 
            case 4:{
                cin>>title;
                cin>>company;
                O_Teacher ot(name,sex,b_date,e_date,dept,salary,title,company);
                L.Add(ot);    
                break;
            }
        }        
    }
    L.printAll();
    return 0;
}

输入样例:

2
李晓敏 f 1986-6-7 2011-7-1 应用数学 5500 教务员
1
黄柏松 m 1980-3-27 2008-7-1 计算机  7500 教授
3
王大明 m 1988-4-25 2018-7-1 管理学院 8500 教授 院长
4
张小兰 f 1985-6-26 2021-7-1 计算机 5000 副教授  兰州大学
1
李达国 m 1988-4-25 2018-7-1 政法学院 8500 教授
0

输出样例:

所有员工信息如下:
No 1 行政人员
姓名:李晓敏
性别:女
出生日期:1986年06月07日
入职日期:2011年07月01日
部门:应用数学
工资:5500
职位:教务员

No 2 专任教师
姓名:黄柏松
性别:男
出生日期:1980年03月27日
入职日期:2008年07月01日
部门:计算机
工资:7500
职称:教授

No 3 双挑人员
姓名:王大明
性别:男
出生日期:1988年04月25日
入职日期:2018年07月01日
部门:管理学院
工资:8500
职称:教授
职位:院长

No 4 外聘教师
姓名:张小兰
性别:女
出生日期:1985年06月26日
入职日期:2021年07月01日
部门:计算机
工资:5000
职称:副教授
单位:兰州大学

No 5 专任教师
姓名:李达国
性别:男
出生日期:1988年04月25日
入职日期:2018年07月01日
部门:政法学院
工资:8500
职称:教授

--------------------------------------------------------------------------------------------------------------------------------

6-7答案

/*从这里开始是答案*/
#include <iostream>
#include <iomanip> 
using namespace std;
#include <string>
#include <vector>
class Date
{
protected:
    int year;
    int month;
    int day;
public:
    Date(int _year, int _month, int _day) : year(_year), month(_month), day(_day)
    {}
    void show()
    {
        cout << year << "年" << setfill('0') << setw(2) << month << "月" << setfill('0') << setw(2) << day << "日" << endl;
    }
};
class Staff
{
protected:
    string name;
    char sex;
    Date B_date;
    Date E_date;
    string dept;
    double salary;
    string sexP;
public:
    Staff(string n, char s, Date b, Date e, string d, double sa) :name(n), sex(s), B_date(b), E_date(e), dept(d), salary(sa)
    {
        if (sex == 'f')
        {
            sexP = "女";
        }
        if (sex == 'm')
        {
            sexP = "男";
        }
    }
    virtual void show()
    {
        cout << "姓名:" << name << endl;
        cout << "性别:" << sexP << endl;
        cout << "出生日期:";
        B_date.show();
        cout << "入职日期:";
        E_date.show();
        cout << "部门:" << dept << endl;
        cout << "工资:" << salary << endl;
    }
};
class Teacher :virtual public Staff
{
protected:
    string title;
public:
    Teacher(string n, char s, Date b, Date e, string d, double sa, string t) :Staff(n, s, b, e, d, sa), title(t) {}
    void show()
    {
        cout << "专任教师" << endl;
        Staff::show();
        cout << "职称:" << title << endl;
    }
};
class Ad_Staff :virtual public Staff
{
protected:
    string postation;
public:
    Ad_Staff(string n, char s, Date b, Date e, string d, double sa, string p) :Staff(n, s, b, e, d, sa), postation(p) {}
    void show()
    {
        cout << "行政人员" << endl;
        Staff::show();
        cout << "职位:" << postation << endl;
    }
};
class Tea_Ad_Staff :public Ad_Staff, public Teacher
{
public:
    Tea_Ad_Staff(string n, char s, Date b, Date e, string d, double sa, string t, string p) :Staff(n, s, b, e, d, sa), Teacher(n, s, b, e, d, sa, t), Ad_Staff(n, s, b, e, d, sa, p) {}
    void show()
    {
        cout << "双挑人员" << endl;
        Staff::show();
        cout << "职称:" << title << endl;
        cout << "职位:" << postation << endl;
    }
};
class O_Teacher :public Teacher
{
protected:
    string company;
public:
    O_Teacher(string n, char s, Date b, Date e, string d, double sa, string t, string c) :Staff(n, s, b, e, d, sa), Teacher(n, s, b, e, d, sa, t), company(c) {}
    void show()
    {
        cout << "外聘教师" << endl;
        Staff::show();
        cout << "职称:" << title << endl;
        cout << "单位:" << company << endl;
    }
};
class Staff_list 
{
protected:
    int num = 0;
public:
    Staff_list()
    {
        cout << "所有员工信息如下:" << endl;
    }
    void Add(Teacher t)
    {
        ++num;
        cout << "No " << num << " ";
        t.show();
        cout << endl;
    }
    void Add(Ad_Staff t)
    {
        ++num;
        cout << "No " << num << " ";
        t.show();
        cout << endl;
    }
    void Add(Tea_Ad_Staff t)
    {
        ++num;
        cout << "No " << num << " ";
        t.show();
        cout << endl;
    }
    void Add(O_Teacher t)
    {
        ++num;
        cout << "No " << num<<" ";
        t.show();
        cout << endl;
    }
    void printAll(){}
};
/*到这里结束 */
int main()
{
    int n;
    //     姓名 部门 职称   职位      单位
    string name,dept,title,postation,company;
    //sex性别
    char sex,c;
    int byear,bmonth,bday,eyear,emonth,eday;    
    double salary;
    Staff_list L;
    while(cin>>n && n!=0)
    {
        cin>>name>>sex;        
        cin>>byear>>c>>bmonth>>c>>bday;
        cin>>eyear>>c>>emonth>>c>>eday;
        cin>>dept>>salary;
        // e_date入职日期 b_date 出生日期
        Date e_date(eyear,emonth,eday),b_date(byear,bmonth,bday);
        switch(n)
        {
            //添加教师 
            case 1:{                            
                cin>>title;
                Teacher t(name,sex,b_date,e_date,dept,salary,title);
                L.Add(t);                
                break;
            }
            //添加行政人员 
            case 2:{
                cin>>postation;
                Ad_Staff as(name,sex,b_date,e_date,dept,salary,postation);
                L.Add(as);                
                break;
            }
            //添加双挑人员 
            case 3:{
                cin>>postation;
                cin>>title;
                Tea_Ad_Staff tas(name,sex,b_date,e_date,dept,salary,postation,title);
                L.Add(tas);                
                break;
            }
            //添加外聘教师 
            case 4:{
                cin>>title;
                cin>>company;
                O_Teacher ot(name,sex,b_date,e_date,dept,salary,title,company);
                L.Add(ot);    
                break;
            }
        }        
    }
    L.printAll();
    return 0;
}

 因为实在想不出来怎么做到两个函数,一个记录一个输出。所以只能面向结果编程了。

6-8 编写Find函数

作者 刘利

单位 惠州学院

在1~6的基础上完成:

实现函数void Find( const Staff * const *L,string name,int num), 从L中查找是否存在name,存在则输出此人信息,不存在则输出“查无此人!".
class Date;
class Staff;
class Teacher;
class Ad_Staff:;
class Tea_Ad_Staff;
class O_Teacher;
以上类代码用前面1~6题自己定义的类。

函数接口定义:

void Find( const Staff * const *L,string name,int num)

L 是Staff类型的常量指针,且指向Staff * 常量。
name 找人的姓名。
num L中元素个数。

注意

不要改变Find实参的类型

语法知识点

1.思考当形参是常量时,对应的代码需要做如何调整?
2.编写代码时如果成员函数不对形参修改,那么最好什么为***成员函数?

裁判测试程序样例:

/* 请在这里填写答案 */
int main()
{
    int n;
    //     姓名 部门 职称   职位      单位
    string name,dept,title,postation,company;
    //sex性别
    char sex,c;
    int byear,bmonth,bday,eyear,emonth,eday;    
    double salary;
    Staff *L[100];
    int num=0;
    while(cin>>n && n!=0)
    {
        cin>>name>>sex;        
        cin>>byear>>c>>bmonth>>c>>bday;
        cin>>eyear>>c>>emonth>>c>>eday;
        cin>>dept>>salary;
        // e_date入职日期 b_date 出生日期
        Date e_date(eyear,emonth,eday),b_date(byear,bmonth,bday);
        switch(n)
        {
            //添加教师 
            case 1:{                            
                cin>>title;
                L[num++]=new Teacher(name,sex,b_date,e_date,dept,salary,title);                            
                break;
            }
            //添加行政人员 
            case 2:{
                cin>>postation;
                L[num++]=new Ad_Staff(name,sex,b_date,e_date,dept,salary,postation);                            
                break;
            }
            //添加双挑人员 
            case 3:{
                cin>>postation;
                cin>>title;
                L[num++]=new Tea_Ad_Staff(name,sex,b_date,e_date,dept,salary,postation,title);                            
                break;
            }
            //添加外聘教师 
            case 4:{
                cin>>title;
                cin>>company;
                L[num++]=new O_Teacher(name,sex,b_date,e_date,dept,salary,title,company);                
                break;
            }
        }        
    }
    cin>>name;
    Find(L,name,num); 
    return 0;
}

输入样例1:

2
李晓敏 f 1986-6-7 2011-7-1 应用数学 5500 教务员
1
黄柏松 m 1980-3-27 2008-7-1 计算机  7500 教授
3
王大明 m 1988-4-25 2018-7-1 管理学院 8500 教授 院长
4
张小兰 f 1985-6-26 2021-7-1 计算机 5000 副教授  兰州大学
1
李达国 m 1988-4-25 2018-7-1 政法学院 8500 教授
0
黄柏松

输出样例1:

黄柏松的信息如下:
专任教师
姓名:黄柏松
性别:男
出生日期:1980年03月27日
入职日期:2008年07月01日
部门:计算机
工资:7500
职称:教授

输入样例2:

2
李晓敏 f 1986-6-7 2011-7-1 应用数学 5500 教务员
1
黄柏松 m 1980-3-27 2008-7-1 计算机  7500 教授
3
王大明 m 1988-4-25 2018-7-1 管理学院 8500 教授 院长
4
张小兰 f 1985-6-26 2021-7-1 计算机 5000 副教授  兰州大学
1
李达国 m 1988-4-25 2018-7-1 政法学院 8500 教授
0
黄柏

输出样例2:

查无此人!

--------------------------------------------------------------------------------------------------------------------------------

6-8答案

/* 从这里开始 */
#include <iostream>
#include <iomanip> 
using namespace std;
#include <string>
class Date
{
protected:
    int year;
    int month;
    int day;
public:
    Date(int _year, int _month, int _day) : year(_year), month(_month), day(_day)
    {}
    void show() const
    {
        cout << year << "年" << setfill('0') << setw(2) << month << "月" << setfill('0') << setw(2) << day << "日" << endl;
    }
};
class Staff
{
protected:
    string name;
    char sex;
    Date B_date;
    Date E_date;
    string dept;
    double salary;
    string sexP;
public:
    Staff(string n, char s, Date b, Date e, string d, double sa) :name(n), sex(s), B_date(b), E_date(e), dept(d), salary(sa)
    {
        if (sex == 'f')
        {
            sexP = "女";
        }
        if (sex == 'm')
        {
            sexP = "男";
        }
    }
    virtual void show() const
    {
        cout << "姓名:" << name << endl;
        cout << "性别:" << sexP << endl;
        cout << "出生日期:";
        B_date.show();
        cout << "入职日期:";
        E_date.show();
        cout << "部门:" << dept << endl;
        cout << "工资:" << salary << endl;
    }
    virtual string getname() const
    {
        return name;
    }
};
class Teacher :virtual public Staff
{
protected:
    string title;
public:
    Teacher(string n, char s, Date b, Date e, string d, double sa, string t) :Staff(n, s, b, e, d, sa), title(t) {}
    void show() const
    {
        cout << "专任教师" << endl;
        Staff::show();
        cout << "职称:" << title << endl;
    }
    string getname() const
    {
        return name;
    }
};
class Ad_Staff :virtual public Staff
{
protected:
    string postation;
public:
    Ad_Staff(string n, char s, Date b, Date e, string d, double sa, string p) :Staff(n, s, b, e, d, sa), postation(p) {}
    void show() const
    {
        cout << "行政人员" << endl;
        Staff::show();
        cout << "职位:" << postation << endl;
    }
    string getname() const
    {
        return name;
    }
};
class Tea_Ad_Staff :public Ad_Staff, public Teacher
{
public:
    Tea_Ad_Staff(string n, char s, Date b, Date e, string d, double sa, string t, string p) :Staff(n, s, b, e, d, sa), Teacher(n, s, b, e, d, sa, t), Ad_Staff(n, s, b, e, d, sa, p) {}
    void show() const
    {
        cout << "双挑人员" << endl;
        Staff::show();
        cout << "职称:" << title << endl;
        cout << "职位:" << postation << endl;
    }
    string getname() const
    {
        return name;
    }
};
class O_Teacher :public Teacher
{
protected:
    string company;
public:
    O_Teacher(string n, char s, Date b, Date e, string d, double sa, string t, string c) :Staff(n, s, b, e, d, sa), Teacher(n, s, b, e, d, sa, t), company(c) {}
    void show() const
    {
        cout << "外聘教师" << endl;
        Staff::show();
        cout << "职称:" << title << endl;
        cout << "单位:" << company << endl;
    }
    string getname() const
    {
        return name;
    }
};
void Find(const Staff* const* L, string name, int num)
{
    int k;
    for (int i = 0; i < num; i++)
    {
        if (name == L[i]->getname())
        {
            cout << name << "的信息如下:" << endl;
            L[i]->show();
            k = i - 1;
            break;
        }
        k = i;
    }
    if (k == num - 1)
    {
        cout << "查无此人!";
    }
}
/* 到这里结束 */
int main()
{
    int n;
    //     姓名 部门 职称   职位      单位
    string name,dept,title,postation,company;
    //sex性别
    char sex,c;
    int byear,bmonth,bday,eyear,emonth,eday;    
    double salary;
    Staff *L[100];
    int num=0;
    while(cin>>n && n!=0)
    {
        cin>>name>>sex;        
        cin>>byear>>c>>bmonth>>c>>bday;
        cin>>eyear>>c>>emonth>>c>>eday;
        cin>>dept>>salary;
        // e_date入职日期 b_date 出生日期
        Date e_date(eyear,emonth,eday),b_date(byear,bmonth,bday);
        switch(n)
        {
            //添加教师 
            case 1:{                            
                cin>>title;
                L[num++]=new Teacher(name,sex,b_date,e_date,dept,salary,title);                            
                break;
            }
            //添加行政人员 
            case 2:{
                cin>>postation;
                L[num++]=new Ad_Staff(name,sex,b_date,e_date,dept,salary,postation);                            
                break;
            }
            //添加双挑人员 
            case 3:{
                cin>>postation;
                cin>>title;
                L[num++]=new Tea_Ad_Staff(name,sex,b_date,e_date,dept,salary,postation,title);                            
                break;
            }
            //添加外聘教师 
            case 4:{
                cin>>title;
                cin>>company;
                L[num++]=new O_Teacher(name,sex,b_date,e_date,dept,salary,title,company);                
                break;
            }
        }        
    }
    cin>>name;
    Find(L,name,num); 
    return 0;
}

 

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值