类的多态性实现

本科生和研究生成绩管理系统

某大学班级有本科生CStudent、留学生FStudent,他们的绩点计算方法如下:

        本科生绩点计算方法是: 英语*0.03+数学*0.04+计算机*0.03

留学生绩点计算方法:  中文*0.03+数学*0.04+计算机*0.03

成绩均为百分制,绩点总计10

每类学生都有学号、姓名、性别、年龄、类别等数据,类别分为本科生和留学生,各类人员使用统一接口getPoint()计算各类人员的绩点,重载<<运算符实现学生信息的输出。其次,设计一个统计并输出该班级每个人员绩点信息的报表类Report,该类提供insert接口向Report类的容器中添加学生信息,并提供print接口用于输出所有学生的学号、姓名、性别、年龄和绩点。为了方便实现查找功能,为Report类重载[]运算符的功能,下标值为学生类别,能根据类别查找出所有该类别的学生,并重载print接口,输出指定类别的学生信息。初始成绩表里可以先固定添加4个本科生和4个留学生信息。各个指令和对应功能如下:

指令1:输入学号,删除相应记录;

指令2:删除所有学生的记录;

指令3:分类别显示所有学生绩点记录表;

指令4:输入类别,显示该类别学生绩点记录表;

指令-1:退出。

//class.h

#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED

#include<windows.h>
#include<iostream>
#include<string>
#include<list>
#include<vector>
#include<map>
#include"date.h"
using namespace std;
class Person
{
protected:
    string ID;//学号
    string name;//姓名
    string sex;//性别
    string type;//学生类型
    int age;
    
    double score;
public:
    Person(string ID, string name, string sex, int age,string type, double score);
    string gettype() { return type; }
    string getname() { return name; }
    string getID() { return ID; }
    string getsex() { return sex; }
    int getage() { return age; }
    double getscore() 
    { 
        return score; 
    }
    virtual double getp(int m,int i,int j) = 0;
    //friend ostream & operator<<(ostream & os,const Person & p);

};

class undergraduate :public Person
{
/*private:
    map<int>score;
*/
private:
    map<int,int> grade1;
public:
    undergraduate(string ID, string name, string sex, int age, string type, double score, map<int,int> grade1) :Person(ID, name, sex, age, type, score) { this->grade1 = grade1; }
    //double getscore(int m);
    double getp(int m,int i,int j);
};
class graduate :public Person
{
private:
    map<int,int> grade;
public:
    graduate(string  ID, string name, string sex, int age, string type, double score, map<int,int> grade) :Person(ID, name, sex, age, type, score) { this->grade = grade; }
    //double getscore(int m);
    double getp(int m,int i,int j);
};
class Report
{
private:
    list<Person*> members;
    list<Person*> operator[](string type);
    double min_score(list<Person*> emp_list);
    double max_score(list<Person*> emp_list);
    void print(list<Person*> emp_list);

public:
    //~Report();
    void insert(Person* p);
    void print(string type);
    void delete_allstudent();//删除所有人的信息
    void delete_student();//根据学号删除信息
};


#endif
//functions.cpp

#include"class.h"
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

Person::Person(string ID, string name, string sex, int age, string type, double score)
{
    this->ID = ID;
    this->name = name;
    this->sex = sex;
    this->age = age;
    this->score = score;
    this->type = type;
}

list<Person*> Report::operator[](string type)
{
    list<Person*> cp;
    list<Person*>::iterator it;
    for (it = members.begin(); it != members.end(); it++)
    {
        if ((*it)->gettype() == type)
        {
            cp.push_back(*it);
        }
    }
    return cp;
}
void Report::print(string t)
{
    cout << "类别:" << t << endl;
    cout << "学号\t" << "姓名\t" << "性别\t" << "年龄\t"<< "绩点" << endl;
    list<Person*> emp_ls;
    emp_ls = (*this)[t];
    print(emp_ls);
    cout << "最低绩点:" << min_score(emp_ls) << endl;
    cout << "最高绩点:" << max_score(emp_ls) << endl;
    cout << "--------------------------------" << endl;


}
double Report::min_score(list<Person*>emp_list)
{
    vector<double>score;
    list<Person*>::iterator it;
    for (it = emp_list.begin(); it != emp_list.end(); it++)
    {
        score.push_back((*it)->getp(1,2,3));
    }
    return *min_element(score.begin(), score.end());
}

double Report::max_score(list<Person*>emp_list)
{
    vector<double> score;
    list<Person*>::iterator it;
    for (it = emp_list.begin(); it != emp_list.end(); it++)
    {
        score.push_back((*it)->getp(1,2,3));
    }
    return *max_element(score.begin(), score.end());
}


void Report::print(list<Person*> emp_list)
{
    list<Person*>::iterator it;
    for (it = emp_list.begin(); it != emp_list.end(); it++)
    {
        cout << (*it)->getID() << "\t" << (*it)->getname() << "\t";
        cout << (*it)->getsex() << "\t";
        cout << (*it)->getage() << "\t";
        cout << (*it)->getp(1,2,3) << "\t";
        cout << endl;
        //cout << (*it)->getscore(type) << endl;
    }
}

void Report::insert(Person* p)
{
    members.push_back(p);
}
void Report::delete_allstudent()
{
    list<Person*>::iterator it;
    for (it = members.begin(); it != members.end(); it++)
    {
        delete* it;
    }
    cout << "所有人的信息删除成功!" << endl;
}
void Report::delete_student()
{
    string id;
    string type;
    cout << "请输入学生的ID:";
    cin >> id;
    cout << "请输入学生类别(本科生or研究生):";
    cin >> type;
    list<Person*>::iterator it;
    //for (it = members.begin(); it != members.end(); it++)
    //{
        if (type == "本科生") {
            for (it = members.begin(); it != members.end(); it++)
            {
                if (strcmp("it->getID()", "id")) {
                    cout << "已成功找到该生信息!" << endl;
                    char ch;
                    cout << "是否确定删除(y or n):";
                    cin >> ch;
                    if (ch == 'y' || ch == 'Y')
                    {
                        members.erase(it++);
                        cout << "删除成功!" << endl;
                        system("pause");
                        return;
                    }
                }
                else {
                    it++;
                }
            }
        }
        else if (type == "研究生") {
            for (it = members.begin(); it != members.end(); it++)
            {
                if (strcmp("it->getID()", "id")){
                    cout << "已成功找到该生信息!" << endl;
                    char ch;
                    cout << "是否确定删除(y or n):";
                    cin >> ch;
                    if (ch == 'y' || ch == 'Y')
                    {
                        members.erase(it++);
                        cout << "删除成功!" << endl;
                        system("pause");
                        return;
                    }
                }
                else {
                    it++;
                }
            }
        }
        else {
            cout << "该生类别不存在,请核实后输入..." << endl;
            system("pause");
            return;
        }
        
    
}
double undergraduate::getp(int m,int i,int j) {
    return  score + grade1[m] * 0.03 + grade1[i] * 0.04 + grade1[j] * 0.03;
}
double graduate::getp(int m,int i,int j) {
    
    return score + grade[m] * 0.03+grade[i]*0.04+grade[j]*0.03;
}


//main.cpp

#include"class.h"
//#include"date.h"
#include"interface.h"
#include<map>
#include<iostream>
using namespace std;
int main()
{
    //Interface inter;
    Report re;
    map<int, int> grade1;
    grade1[1] = 98;
    grade1[2] = 97;
    grade1[3] = 96;
    re.insert(new undergraduate("001", "张三", "男", 19, "本科生", 0,grade1));
    map<int, int> grade;
    grade1[1] = 99;
    grade1[2] = 96;
    grade1[3] = 95;
    re.insert(new undergraduate("002", "李四", "女", 17, "本科生", 0, grade1));
    grade1[1] = 94;
    grade1[2] = 92;
    grade1[3] = 91;
    re.insert(new undergraduate("003", "祝雪", "女", 16, "本科生", 0, grade1));
    grade1[1] = 89;
    grade1[2] = 96;
    grade1[3] = 91;
    re.insert(new undergraduate("004", "祝贺", "男", 18, "本科生", 0, grade1));
    grade[1] = 90;
    grade[2] = 91;
    grade[3] = 98;
    re.insert(new graduate("101", "Tom", "男", 20, "研究生", 0, grade));
    grade[1] = 92;
    grade[2] = 91;
    grade[3] = 90;
    re.insert(new graduate("102", "Jack", "男", 19, "研究生", 0, grade));
    grade[1] = 99;
    grade[2] = 93;
    grade[3] = 88;
    re.insert(new graduate("103", "Lusy", "女", 18, "研究生", 0, grade));
    grade[1] = 90;
    grade[2] = 86;
    grade[3] = 85;
    re.insert(new graduate("104", "Mike", "男", 19, "研究生", 0, grade));
    
    int i;
    cout << "-------------------------------------" << endl;
    cout << "操作菜单项" << endl;
    cout << "输入1,输入学号,删除相应记录" << endl;
    cout << "输入2,删除所有学生记录" << endl;
    cout << "输入3,分类别显示所有学生绩点记录表" << endl;
    cout << "输入4,输入类别,显示该类别学生绩点记录表" << endl;
    cout << "输入-1,退出" << endl;
    cout << "-------------------------------------" << endl;
    
    while (1) {
        cout << "输入您的选择:" << "i=" << endl;
        cin >> i;
        if (i == -1) {
            cout << "退出成绩管理系统" << endl;
            break;
        }
        if (i == 1) {
            re.delete_student();//根据学号删除学生信息
        }
        if (i == 2) {
            re.delete_allstudent();//删除所有人的信息
        }
        if (i == 3) {
            cout << "分别显示绩点记录表" << endl;
            cout << "---------------------------" << endl;
            re.print("本科生");

            re.print("研究生");
        }
        if (i == 4) {
            cout << "按类别显示绩点记录表" << endl;
            cout << "请输入类别(本科生or研究生):" << endl;;
            string type;
            cin >> type;
            if (type == "本科生") {

                re.print("本科生");
            }
            else if (type == "研究生") {

                re.print("研究生");
            }
            else {
                cout << "您输入有误,请重新输入" << endl;
            }
        }
    }

    return 0;

}

运行结果展示

1、显示所有学生的信息(功能三)

2、分类别显示学生的信息(按照输入类别显示学生信息)

(1)输入本科生

(2)输入研究生

3、根据学号删除学生信息(例删除本科生学号001)

4、删除所有人的信息

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
某公司的员工有经理Manager、技术人员Technicist和营销人员SalsePerson,他们的薪金计算方法如下: 经理按月计酬,方法是:基本工资+奖金;技术人员按月计酬,方法是:基本工资;营销人员按月计酬,方法是:基本工资+销售利润*5%。 每人员都有职工编号、姓名、性别、入职时间、职位、基本工资等数据,其中为入职时间定义Date,并为该重载运算符<<,实现入职时间的输入;各人员使用统一接口getpay()计算各人员的月薪。其次,设计一个统计并输出该公司每个人员某几个月薪金情况的报表Report,该提供add接口向Report的容器中添加员工信息,并提供print接口用于输出每个员工的职工编号、姓名、性别、入职时间、职位和在设定的月份时间段中该员工的薪酬总额。为了方便实现查找功能,为Report重载[]运算符的功能,下标值为职位,能根据职位信息查找出所有符合该职位的员工,并重载print接口,输出查找出的员工信息,信息包括职工编号、姓名、性别、入职时间、职位、基本工资。在主函数中对实现进行测试,首先,创建各人员对象,通过Report的add接口向报表中添加这些人员信息,然后通过Report的print接口输出报表。其次测试报表的查找功能,输入要查找的员工职位信息,通过Report的print接口输出查找到的员工基本信息报表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值