欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

厦大计算机系C++程序设计实验(三)

程序员文章站 2022-04-21 12:02:08
...

1 实验目的

(1)理解类层次的概念及实现类层次的方法,掌握派生的类别与方式。

(2)掌握派生类中如何使用基类的成员、基类成员在派生类中的访问控制。

(3)理解派生类中构造函数与析构函数的定义方法和执行顺序。

2 实验内容

(1)按照下面的类层次图要求编写程序。

厦大计算机系C++程序设计实验(三)

注:Tutor类的student成员为层次图中的student类。

每个类都有input()和print()成员函数。分别定义类Tutor和ST_Score的对象,分别输入若干个数据成员的值再显示出这些数据。

(2)修改ST_Score类,添加成员函数用于计算成绩平均值。

3 实验步骤

自上而下的实现各个类,首先实现Person类,再通过继承Person类实现Teacher类和Student类,再通过继承Teacher类并使用Student类实现Tutor类,最后通过继承Student类实现ST_Score类。

对于每个类,其数据域为私有成员,其成员函数input()和print()为公有成员。底层的子类的派生方式均为公有继承。同时,在ST_Score类中增添average_score变量和average()函数来计算平均值。

以下为具体代码:

#include <iostream>

using namespace std;

class Person
{
public:
    /**Input person's name and id number*/
    void input(void);
    /**Print out person's name and id number in one line*/
    void print(void);
private:
    /**Person's name*/
    string name;
    /**Person's id number*/
    string id;
};

class Teacher: public Person
{
public:
    /**Input teacher's personal information and his/her degree and department*/
    void input(void);
    /**Output teacher's personal information and his/her degree and department*/
    void print(void);
private:
    /**Teacher's degree(Bachelor, Master, Doctor)*/
    string degree;
    /**Teacher's department*/
    string Dep;
};

class Student: public Person
{
public:
    /**Input student's personal info. and his/her age and NO.*/
    void input(void);
    /**Input student's personal info. and his/her age and NO.*/
    void print(void);
private:
    /**Student's age*/
    int age;
    /**Student's No.*/
    long long stuNum;
};

class Tutor: public Teacher
{
public:
    /**Input tutor's and student's personal info., and their meeting time*/
    void input(void);
    /**Print out tutor's and student's personal info., and their meeting time*/
    void print(void);
private:
    /**Tutor's student*/
    Student student;
    /**Tutor and his/her student's meeting time, including hour and minute,
       if input date is invalid, then let it be 0*/
    struct
    {
        int hour;
        int minute;
    }meetingTime;
};

class ST_Score: public Student
{
public:
    /**Input math and English score, if input data is less than 0 or bigger than 100, let it be 0*/
    void input(void);
    /**Print out student's math and English score in one line*/
    void print(void);
    /**Calculate his/her average score*/
    void average(void);
private:
    /**Math score*/
    double math;
    /**English score*/
    double english;
    /**Average score*/
    double average_score;
};

void Person::input(void)
{
    cout << "Please input his/her name: ";
    cin >> name;

    cout << "Please input his/her id number: ";
    cin >> id;
}

void Person::print(void)
{
    cout << "Name: " << name <<endl
         << "Id: " << id << endl;
}

void Teacher::input(void)
{
    Person::input();

    cout << "Please input teacher's degree: ";
    cin >> degree;
    cout << "Please input teacher's department: ";
    cin >> Dep;
}

void Teacher::print(void)
{
    Person::print();

    cout << "Degree: " << degree << endl
         << "Department: " << Dep << endl;
}

void Student::input(void)
{
    Person::input();

    cout << "Please input student's age: ";
    cin >> age;
    cout << "Please input student's NO.: ";
    cin >> stuNum;
}

void Student::print(void)
{
    Person::print();

    cout << "Age: " << age << endl
         << "NO.: "<< stuNum << endl;
}

void Tutor::input(void)
{
    cout << "Teacher's info.: " << endl;
    Teacher::input();
    cout << "His/her student's info.: " << endl;
    student.input();

    int new_hour, new_minute;
    cout << "Please input their meeting time(e.g. 14:30): ";
    cin >> new_hour;
    cin.get();
    cin >> new_minute;
    if(0 <= new_hour && new_hour < 24)
        meetingTime.hour = new_hour;
    else
        meetingTime.hour = 0;
    if(0 <= new_minute && new_hour < 60)
        meetingTime.minute = new_minute;
    else
        meetingTime.minute = 0;
}

void Tutor::print(void)
{
    Teacher::print();
    student.print();

    cout << "Their meeting time is: " << meetingTime.hour << ":" << meetingTime.minute << endl;
}

void ST_Score::input(void)
{
    Student::input();

    double math_score, english_score;

    cout << "Please input student's math score: ";
    cin >> math_score;
    cout << "Please input student's English score: ";
    cin >> english_score;

    if(0 <= math_score && math_score <= 100)
        math = math_score;
    else
        math = 0;
    if(0 <= english_score && english_score <= 100)
        english = english_score;
    else
        english = 0;

    average();
}

void ST_Score::print(void)
{
    Student::print();

    cout << "Math score: " << math << endl
         << "English score: " << english << endl
         << "Average score: " << average_score << endl;
}

void ST_Score::average(void)
{
    average_score = (math + english) / 2.0;
}

/**Main function*/
int main()
{
    Tutor tutor;
    ST_Score stu_score;

    cout << "Tutor: " << endl;
    tutor.input();
    cout << endl << "Tutor's info.: " << endl;
    tutor.print();

    cout << endl << "Student: " << endl;
    stu_score.input();
    cout << endl << "Student's info.: " << endl;
    stu_score.print();

    return 0;
}

4 实验结果

若输入信息为:

Tutor:
Teacher's info.:
Please input his/her name: LiuXiangrong
Please input his/her id number: 223110197905234523
Please input teacher's degree: Doctor
Please input teacher's department: ComputerScience
His/her student's info.:
Please input his/her name: LiuLizhi
Please input his/her id number: 340111199612131230
Please input student's age: 19
Please input student's NO.: 22920142203873
Please input their meeting time(e.g. 14:30): 15:34

Student:
Please input his/her name: LiuLizhi
Please input his/her id number: 340111199612131230
Please input student's age: 19
Please input student's NO.: 22920142203873
Please input student's math score: 88.5
Please input student's English score: 85

输出结果为:

Tutor's info.:
Name: LiuXiangrong
Id: 223110197905234523
Degree: Bachelor
Department: ComputerScience
Name: LiuLizhi
Id: 340111199612131230
Age: 19
NO.: 22920142203873
Their meeting time is: 15:34

Student's info.:
Name: LiuLizhi
Id: 340111199612131230
Age: 19
NO.: 22920142203873
Math score: 88.5
English score: 85
Average score: 86.75

以下为运行截图:

厦大计算机系C++程序设计实验(三)

厦大计算机系C++程序设计实验(三)

5 心得小结

通过本次实验,我学会了如何继承类,通过继承实现类的层次实现,并且掌握了基类、派生类内各成员函数及变量的使用方法和注意事项。在实际编写程序时,遇到了这样几个问题:

1、会面时间meetingTime的结构体声明出现错误,应该将名字至于句尾花括号和分号之间,而非在关键字struct之后;

2、在修改ST_Score类时,没有在input()函数中添加average()函数,结果没有计算平均分,而输出随机值。

通过这次实验,我还了解了使用类层次图的优点。通过图示可以清晰的编写程序,而不会出现混乱,以至于出现许多bugs。

相关标签: Cpp 厦大