C++实现多态:某大学班级有本科生CStudent、留学生FStudent,为了方便实现查找功能,为Report类重载[]运算符的功能,下标值为学生类别,能根据类别查找所有该类别学生输出指定类别信息。

浙江理工大学信息学院

实验指导书

实验名称:类的多态性的实现                   学时安排:3

实验类别:设计性实验                              实验要求:1人1组 

学号:2020329621193                             姓名:杨正龙

 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄

一、实验目的

1.理解重载运算符的意义。

2.掌握使用友元函数重载运算符的特点。

3.掌握重载运算符函数的调用方法。

4.掌握动态联编的概念。

5.掌握虚函数和纯虚函数的使用方法。

二、实验原理介绍

设计性实验

具体原理请见实验内容和步骤

实现对抽象类的继承,通过operator函数调用的形式,实现运算符的重载

三、实验设备介绍

软件需求: Visual Studio C++或Codeblocks或Dev C++或其他C++ IDE

硬件需求: 能够流畅运行C++ IDE的计算机一台。

四、实验内容

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

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

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

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

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

初始成绩表里可以先固定添加4个本科生和4个留学生信息。各个指令和对应功能如下:

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

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

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

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

指令-1:退出。

五、程序清单

function.h:

#pragma once

#include <iostream>

#include"Student.h"

#include"vector"



class Report {

private:

    std::vector<Student*> Stu;//使用vector存储Student指针

public:

    void insert(Student*);//插入元素

    bool Pdelete(string sno);//删除特定学生的信息

    void Fdelete();//删除全部学生的信息

    void operator[](string)const;//查找特定类型学生的信息

    void print(string);//输出全部学生的信息

    void print(int);//输出本科生的信息

    void print(double);//输出留学生的信息

};

Student.h:

#pragma once

#include<iostream>

using std::string;



class Student {

private:

    string num, name, types, sex; //学号,姓名,类型,性别

    int age;                      //年龄

    double math, computer;        //数学成绩,计算机成绩

public:

    Student(string, string, string, string, int, double, double);//初始化

    virtual double getPoint() const = 0;//计算绩点

    friend std::ostream& operator<<(std::ostream&, Student*);//对<<进行运算符重载

    string getnum() const;      //获取学号

    void setnum(std::string);   //修改学号

    string getname() const;     //获取姓名

    void setname(std::string);  //修改姓名

    string gettypes() const;    //获取类别

    void settypes(std::string); //修改类别

    string getsex()const;       //获取性别

    void setsex(string);        //修改性别

    int getage() const;         //获取年龄

    void setage(int);           //修改年龄

    double getmath() const;     //获取数学绩点

    void setmath(double);       //修改数学绩点

    double getcomputer() const; //获取计算机绩点

    void setcomputer(double);   //修改计算机绩点

};



class FStudent :public Student {

private:

    double chinese;//中文成绩

public:

    FStudent(string, string, string, string, int, double, double, double);//初始化信息

    double getPoint() const;//计算绩点

};



class CStudent :public Student {

private:

    double englishi;//英语成绩

public:

    CStudent(string, string, string, string, int, double, double, double);//初始化

    double getPoint()const;//计算绩点

};

Function.cpp:

#include "function.h"

using namespace std;

void Report::insert(Student* S1) {//插入元素

    Stu.push_back(S1);

}

bool Report::Pdelete(string sno) {//删除特定学号的学生信息

    for (int i = 0; i < Stu.size(); ++i) {

        if (Stu[i]->getnum() == sno) {//找到该学生,对该数据进行覆盖

            if (i != Stu.size() - 1) {

                for (int j = i; j < Stu.size() - 1; ++j) {

                    Stu[j] = Stu[j + 1];

                }

            }

            Stu.pop_back();//弹出最后一位

            return true;//找到并删除

        }

    }

    return false;//未找到

}

void Report::operator[](string type)const {//查找特定类型的学生信息

    cout << type << std::endl;

    if (Stu.size() == 0)

        cout << "无学生记录" <<endl;//判断是否有学生

    else {

        double max = -1, min = 10;

        cout << "学号   姓名   性别    年龄    绩点" <<endl;

        for (int i = 0; i < Stu.size(); ++i) {

            if (Stu[i]->gettypes() == type) {//判断是否是此类学生

                cout << Stu[i];

                if (Stu[i]->getPoint() > max)//获取最大绩点

                    max = Stu[i]->getPoint();

                if (Stu[i]->getPoint() < min)//获取最小绩点

                    min = Stu[i]->getPoint();

            }

        }

        if (max < 0)

            cout << "无此类学生" << endl;

        else {

            cout << "最低绩点: " << min << endl;//最低绩点

            cout << "最高绩点: " << max << endl;//最高绩点

        }

    }

}

void Report::Fdelete() {//删除全部学生信息

    Stu.clear();

}



void Report::print(int a) {//输出本科生信息

    operator[]("本科生");

}



void Report::print(double a) {//输出留学生信息

    operator[]("留学生");

}



void Report::print(string a)

{

    operator[]("本科生");

    cout << "------------------------------------------" << endl;

    operator[]("留学生");

}

Student.cpp:

#include"function.h"



Student::Student(string num1, string name1, string types1, string sex1, int age1, double m1, double c1) :num(num1), name(name1), types(types1), sex(sex1), age(age1), math(m1), computer(c1) {



}

string Student::getnum()const {

    return num;

}

void Student::setnum(string num1) {

    num = num1;

}

string Student::getname() const {

    return name;

}

void Student::setname(string name1) {

    name = name1;

}

string Student::gettypes() const {

    return types;

}

void Student::settypes(string type1) {

    types = type1;

}

string Student::getsex()const {

    return sex;

}

void Student::setsex(string sex1) {

    sex = sex1;

}

int Student::getage() const {

    return age;

}

void Student::setage(int age1) {

    age = age1;

}

double Student::getmath() const {

    return math;

}

void Student::setmath(double m1) {

    math = m1;

}

double Student::getcomputer() const {

    return computer;

}

void Student::setcomputer(double c1) {

    computer = c1;

}

std::ostream& operator<<(std::ostream& out, Student* S) {

    out << S->getnum() << "    " << S->getname() << "    " << S->getsex() << "      " << S->getage() << "     " << S->getPoint() << std::endl;

    return out;

}



CStudent::CStudent(string num1, string name1, string types1, string sex1, int age1, double m1, double c1, double e1) :Student(num1, name1, types1, sex1, age1, m1, c1), englishi(e1) {}

double CStudent::getPoint()const {

    return englishi * 0.03 + getmath() * 0.04 + getcomputer() * 0.03;

}



FStudent::FStudent(string num1, string name1, string types1, string sex1, int age1, double m1, double c1, double ch1) : Student(num1, name1, types1, sex1, age1, m1, c1), chinese(ch1) {}

double FStudent::getPoint()const {

    return chinese * 0.03 + getmath() * 0.04 + getcomputer() * 0.03;

}

Main.cpp:

#include <iostream>

#include"function.h"



using namespace std;

int main()

{

    CStudent CS1("001", "张三", "本科生", "男", 20, 99, 78, 89),

             CS2("002", "李四", "本科生", "女", 19, 87, 92, 95),

             CS3("003", "王五", "本科生", "女", 20, 92, 63, 75),

             CS4("004", "赵六", "本科生", "男", 19, 79, 85, 95);



    FStudent FS1("101", "Aboy ", "留学生", "男", 18, 70, 78, 54),

             FS2("102", "Bboy ", "留学生", "男", 19, 89, 90, 87),

             FS3("103", "Cgirl", "留学生", "女", 20, 95, 84, 76),

             FS4("104", "Dboy ", "留学生", "男", 19, 83, 89, 63);



    Report R1;

    R1.insert(&CS1);

    R1.insert(&CS2);

    R1.insert(&CS3);

    R1.insert(&CS4);

    R1.insert(&FS1);

    R1.insert(&FS2);

    R1.insert(&FS3);

    R1.insert(&FS4);

    int op = 0;

    while (op != -1) {

        cout << "------------------------------------------" << endl;

        cout << "操作菜单项" << endl;

        cout << "输入1:输入学号,删除相应记录" << endl;

        cout << "输入2:删除所有学生的记录" << endl;

        cout << "输入3:分类别显示所有学生绩点记录表" << endl;

        cout << "输入4:输入类别,显示该类别学生绩点记录表" << endl;

        cout << "输入-1:退出" << endl;

        cout << "------------------------------------------" << endl;

        cin >> op;

        cin.clear();

        switch (op) {

        case 1: {

            string num;

            cout << "请输入该学生的学号" << endl;

            cin >> num;

            if (R1.Pdelete(num))

                cout << "删除成功!" << endl;

            else

                cout << "无此学生!" << endl;

            cout << "------------------------------------------" << endl;

            break;

        }

        case 2: {

            R1.Fdelete();

            cout << "已全部删除!" << endl;

            cout << "------------------------------------------" << endl;

            break;

        }

        case 3: {

            cout << "显示绩点记录表" << endl;

            cout << "------------------------------------------" << endl;

            R1.print("全部");

            cout << "------------------------------------------" << endl;

            break;

        }

        case 4: {

            string type;

            cout << "按类别显示绩点记录表" << endl;

            cout << "请输入类别: " << endl;

            cin >> type;

            while (type != "本科生" && type != "留学生") {

                cout << "类别输入错误,请重新输入!" << endl;

                cin >> type;

            }

            if (type == "本科生")

                R1.print(1);//本科生

            else

                R1.print(1.1);//留学生

            cout << "------------------------------------------" << endl;

            break;

        }

        case -1: {

            cout << "退出成功!" << endl;

            cout << "------------------------------------------" << endl;

            break;

        }

        default: {

            cout << "操作数输入错误,请重新输入!" << endl;

            cout << "------------------------------------------" << endl;

            break;

        }

        }

    }

    return 0;

}

六、运行结果

 

 

七、实验心得

通过这次实验,我对类的多态有了更深的的理解。刚开始我把CStudent和FStudent直接定义成了两个类,但是之后又进行了更改。此外,在实验中我也实际操作了操作符的重载,并熟悉了容器的使用。我发现使用向量和容器会比直接编程实现功能更加方便,以后可以尽量多用容器。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值