大学生考勤管理系统c++利用类,对象,继承,构造函数,类的组合,多态,代码

时间: 2023-12-14 08:24:02 浏览: 34
以下是一个大学生考勤管理系统c++利用类、对象、继承、构造函数、类的组合、多态的完整代码示例: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; // 学生类 class Student { protected: string id; // 学号 string name; // 姓名 string gender; // 性别 int age; // 年龄 public: Student(string id, string name, string gender, int age) { this->id = id; this->name = name; this->gender = gender; this->age = age; } // 获取学生信息 virtual void getInfo() { cout << "学号:" << id << endl; cout << "姓名:" << name << endl; cout << "性别:" << gender << endl; cout << "年龄:" << age << endl; } // 修改学生信息 virtual void modifyInfo(string name, string gender, int age) { this->name = name; this->gender = gender; this->age = age; } }; // 老师类 class Teacher { protected: string id; // 工号 string name; // 姓名 string course; // 所教课程 public: Teacher(string id, string name, string course) { this->id = id; this->name = name; this->course = course; } // 获取教师信息 virtual void getInfo() { cout << "工号:" << id << endl; cout << "姓名:" << name << endl; cout << "所教课程:" << course << endl; } // 修改学生成绩 virtual void modifyScore(string studentId, string courseId, int score) { // 修改学生课程成绩 } }; // 课程类 class Course { protected: string id; // 课程编号 string name; // 课程名称 int credit; // 学分 public: Course(string id, string name, int credit) { this->id = id; this->name = name; this->credit = credit; } // 获取课程信息 virtual void getInfo() { cout << "课程编号:" << id << endl; cout << "课程名称:" << name << endl; cout << "学分:" << credit << endl; } }; // 考勤类 class Attendance { protected: string date; // 考勤日期 bool status; // 考勤状态 public: Attendance(string date, bool status) { this->date = date; this->status = status; } // 获取考勤信息 virtual void getInfo() { cout << "考勤日期:" << date << endl; cout << "考勤状态:" << (status ? "已到" : "未到") << endl; } }; // 学生选课类 class CourseSelection { protected: Student* student; // 学生指针 Course* course; // 课程指针 vector<Attendance*> attendances; // 考勤列表 public: CourseSelection(Student* student, Course* course) { this->student = student; this->course = course; } // 添加考勤 void addAttendance(Attendance* attendance) { attendances.push_back(attendance); } // 获取学生选课信息 virtual void getInfo() { student->getInfo(); course->getInfo(); for (int i = 0; i < attendances.size(); i++) { attendances[i]->getInfo(); } } }; // 继承学生类的本科生类 class Undergraduate : public Student { private: string major; // 专业 public: Undergraduate(string id, string name, string gender, int age, string major) : Student(id, name, gender, age) { this->major = major; } // 获取学生信息 void getInfo() { Student::getInfo(); cout << "专业:" << major << endl; } }; // 继承学生类的研究生类 class Graduate : public Student { private: string tutor; // 导师 public: Graduate(string id, string name, string gender, int age, string tutor) : Student(id, name, gender, age) { this->tutor = tutor; } // 获取学生信息 void getInfo() { Student::getInfo(); cout << "导师:" << tutor << endl; } }; // 继承老师类的教授类 class Professor : public Teacher { private: string research; // 研究方向 public: Professor(string id, string name, string course, string research) : Teacher(id, name, course) { this->research = research; } // 获取教师信息 void getInfo() { Teacher::getInfo(); cout << "研究方向:" << research << endl; } // 修改学生成绩 void modifyScore(string studentId, string courseId, int score) { // 修改学生课程成绩 } }; int main() { // 创建学生对象 Student* stu = new Undergraduate("1001", "张三", "男", 20, "计算机科学与技术"); // 获取学生信息 stu->getInfo(); // 修改学生信息 stu->modifyInfo("李四", "女", 21); stu->getInfo(); // 创建课程对象 Course* course = new Course("C001", "C++程序设计", 3); // 获取课程信息 course->getInfo(); // 创建考勤对象 Attendance* att = new Attendance("2022-01-01", true); // 获取考勤信息 att->getInfo(); // 创建教师对象 Teacher* teacher = new Professor("T001", "王老师", "C++程序设计", "计算机科学"); // 获取教师信息 teacher->getInfo(); // 修改学生成绩 teacher->modifyScore("1001", "C001", 90); // 创建学生选课对象 CourseSelection* cs = new CourseSelection(stu, course); cs->addAttendance(att); cs->getInfo(); // 释放内存 delete stu; delete course; delete att; delete teacher; delete cs; return 0; } ``` 以上代码实现了大学生考勤管理系统的基本功能,包括学生、课程、考勤、教师、学生选课等类的设计和实现,利用继承、构造函数、类的组合、多态等技术实现了代码的灵活性和可扩展性。

相关推荐

最新推荐

recommend-type

Java实验报告——类的继承、多态的应用

1、实验名称:类的继承、多态的应用 2、代码内容:学生管理类 3、【实验目的】 (1)进一步熟悉抽象类和抽象方法的定义 (2) 进一步掌握集成和多态的实现方式。
recommend-type

Java多态和实现接口的类的对象赋值给接口引用的方法(推荐)

下面小编就为大家带来一篇Java多态和实现接口的类的对象赋值给接口引用的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

c++ 虚函数与纯虚函数的区别(深入分析)

那么,什么是虚函数呢,我们先来看看微软的解释: 虚函数是指一个类中你希望重载的成员函数,当你用一个基类指针或引用指向一个继承类对象的时候,你调用一个虚函数,实际调用的是继承类的版本。
recommend-type

C++图书馆管理程序.docx(带有完整源代码,类的声明和定义分开,非常清晰)

完整版的C++图书馆管理程序,包括对问题的分析,类的设计与声明,还画了类的层次图;运用了继承,和多态,使用了vector,以及文件读取;使用了人员继承和书本继承。
recommend-type

详解C++ 多态的两种形式(静态、动态)

1.多态的概念与分类 多态(Polymorphisn)是面向对象程序...静态多态是指在编译期间就可以确定函数的调用地址,并生产代码,这就是静态的,也就是说地址是早绑定。静态多态往往也被叫做静态联编。 动态多态则是指函数
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。