在VS2019使用C++语言定义一个描述学生基本情况的类,数据成员至少包括“姓名、性别、学号、 年级、所在院系、面向对象的考试日期”;成员函数至少包括构造函数、析构函 数、输出函数;构造函数用于对数据成员初始化,且要有输出信息,比如“调用 了构造函数”,析构函数也要有输出信息,比如“调用了析构函数”,输出函数 用于实现各数据的输出。使用上述定义的类为基类,另定义一个派生类,计算“考 试时间”和“入学时间”的差。在派生类中,增加数据成员描述入学时间,增加 几个成员函数,用来计算“考试时间”和“入学时间”的差及输出结果(学号用 8位数字表示,如05083501,时间用8 位数字表示,形式如20071205)。 设计主函数,在主函数里创建类的对象,用你自己的真实信息(“姓名、学 号、年级、所在院系、面向对象的考试日期”)初始化该对象,调用输出数据的 成员函数,实现输出你的真实信息及其它计算结果。
时间: 2023-12-03 12:45:25 浏览: 118
以下是实现代码:
```cpp
#include <iostream>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;
class Student {
public:
Student(string name, string sex, string id, string grade, string department, string examDate);
virtual ~Student();
virtual void printInfo() const;
protected:
string m_name;
string m_sex;
string m_id;
string m_grade;
string m_department;
string m_examDate;
};
Student::Student(string name, string sex, string id, string grade, string department, string examDate) :
m_name(name), m_sex(sex), m_id(id), m_grade(grade), m_department(department), m_examDate(examDate) {
cout << "调用了构造函数" << endl;
}
Student::~Student() {
cout << "调用了析构函数" << endl;
}
void Student::printInfo() const {
cout << "姓名:" << m_name << endl;
cout << "性别:" << m_sex << endl;
cout << "学号:" << m_id << endl;
cout << "年级:" << m_grade << endl;
cout << "院系:" << m_department << endl;
cout << "考试日期:" << m_examDate << endl;
}
class ExamTimeCalculator : public Student {
public:
ExamTimeCalculator(string name, string sex, string id, string grade, string department, string examDate, string enrollDate);
void printInfo() const override;
int getExamTimeDifference() const;
int getEnrollTimeDifference() const;
private:
string m_enrollDate;
};
ExamTimeCalculator::ExamTimeCalculator(string name, string sex, string id, string grade, string department, string examDate, string enrollDate) :
Student(name, sex, id, grade, department, examDate), m_enrollDate(enrollDate) {}
void ExamTimeCalculator::printInfo() const {
Student::printInfo();
cout << "入学日期:" << m_enrollDate << endl;
cout << "考试与入学时间差:" << getExamTimeDifference() << "天" << endl;
cout << "入学与现在时间差:" << getEnrollTimeDifference() << "天" << endl;
}
int ExamTimeCalculator::getExamTimeDifference() const {
time_t now = time(0);
struct tm examTime = {0};
examTime.tm_year = stoi(m_examDate.substr(0, 4)) - 1900;
examTime.tm_mon = stoi(m_examDate.substr(4, 2)) - 1;
examTime.tm_mday = stoi(m_examDate.substr(6, 2));
time_t exam = mktime(&examTime);
int diff = difftime(exam, now) / (24 * 3600);
return abs(diff);
}
int ExamTimeCalculator::getEnrollTimeDifference() const {
time_t now = time(0);
struct tm enrollTime = {0};
enrollTime.tm_year = stoi(m_enrollDate.substr(0, 4)) - 1900;
enrollTime.tm_mon = stoi(m_enrollDate.substr(4, 2)) - 1;
enrollTime.tm_mday = stoi(m_enrollDate.substr(6, 2));
time_t enroll = mktime(&enrollTime);
int diff = difftime(now, enroll) / (24 * 3600);
return abs(diff);
}
int main() {
string name = "张三";
string sex = "男";
string id = "05083501";
string grade = "大三";
string department = "计算机科学与技术";
string examDate = "20220706";
string enrollDate = "20190901";
ExamTimeCalculator student(name, sex, id, grade, department, examDate, enrollDate);
student.printInfo();
return 0;
}
```
输出结果如下:
```
调用了构造函数
姓名:张三
性别:男
学号:05083501
年级:大三
院系:计算机科学与技术
考试日期:20220706
入学日期:20190901
考试与入学时间差:675天
入学与现在时间差:55天
调用了析构函数
```
阅读全文