使用面向对象C++写一份校园卡管理系统,要求文件输入输出,并定义对象,用对象调用操作函数

时间: 2023-06-19 16:10:16 浏览: 53
好的,以下是一份基本的校园卡管理系统的代码: ```c++ #include <iostream> #include <fstream> #include <string> using namespace std; // 定义学生类 class Student { private: string name; // 姓名 int id; // 学号 float balance; // 余额 public: Student(string name, int id, float balance) { // 构造函数 this->name = name; this->id = id; this->balance = balance; } void deposit(float amount) { // 充值 balance += amount; cout << "充值成功!当前余额为:" << balance << endl; } void consume(float amount) { // 消费 if (balance >= amount) { balance -= amount; cout << "消费成功!当前余额为:" << balance << endl; } else { cout << "余额不足,无法完成消费!" << endl; } } void printInfo() { // 输出学生信息 cout << "姓名:" << name << endl; cout << "学号:" << id << endl; cout << "余额:" << balance << endl; } void writeToFile(ofstream& out) { // 将学生信息写入文件 out << name << endl; out << id << endl; out << balance << endl; } }; // 添加学生 void addStudent(ofstream& out) { string name; int id; float balance; cout << "请输入学生姓名:" << endl; cin >> name; cout << "请输入学生学号:" << endl; cin >> id; cout << "请输入学生初始余额:" << endl; cin >> balance; Student student(name, id, balance); student.writeToFile(out); cout << "添加成功!" << endl; } // 查询学生信息 void queryStudent(ifstream& in) { int id; cout << "请输入要查询的学生学号:" << endl; cin >> id; string name; float balance; bool found = false; while (in >> name >> id >> balance) { if (id == id) { found = true; cout << "学生信息如下:" << endl; cout << "姓名:" << name << endl; cout << "学号:" << id << endl; cout << "余额:" << balance << endl; break; } } if (!found) { cout << "未找到该学生信息!" << endl; } } // 充值 void deposit(ifstream& in, ofstream& out) { int id; float amount; cout << "请输入要充值的学生学号:" << endl; cin >> id; cout << "请输入充值金额:" << endl; cin >> amount; string name; float balance; bool found = false; while (in >> name >> id >> balance) { if (id == id) { found = true; Student student(name, id, balance); student.deposit(amount); student.writeToFile(out); break; } } if (!found) { cout << "未找到该学生信息!" << endl; } } // 消费 void consume(ifstream& in, ofstream& out) { int id; float amount; cout << "请输入要消费的学生学号:" << endl; cin >> id; cout << "请输入消费金额:" << endl; cin >> amount; string name; float balance; bool found = false; while (in >> name >> id >> balance) { if (id == id) { found = true; Student student(name, id, balance); student.consume(amount); student.writeToFile(out); break; } } if (!found) { cout << "未找到该学生信息!" << endl; } } int main() { ofstream out("students.txt", ios::app); // 打开文件,追加写入 out.close(); ifstream in("students.txt"); // 打开文件,读取 if (!in) { cout << "文件打开失败!" << endl; return 0; } int choice; while (true) { cout << "请选择操作:" << endl; cout << "1. 添加学生" << endl; cout << "2. 查询学生信息" << endl; cout << "3. 充值" << endl; cout << "4. 消费" << endl; cout << "5. 退出" << endl; cin >> choice; if (choice == 1) { addStudent(out); } else if (choice == 2) { queryStudent(in); } else if (choice == 3) { deposit(in, out); } else if (choice == 4) { consume(in, out); } else if (choice == 5) { break; } else { cout << "无效选择!" << endl; } } in.close(); // 关闭文件 out.close(); return 0; } ``` 该程序使用了面向对象的编程方式,将学生信息封装在了一个类中,通过创建对象来调用操作函数。同时,也使用了文件输入输出来保存学生信息,可以方便地进行管理和查询。

相关推荐

最新推荐

recommend-type

C++中输入输出流及文件流操作总结

主要为大家总结了C++中输入输出流及文件流操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

javascript使用Blob对象实现的下载文件操作示例

主要介绍了javascript使用Blob对象实现的下载文件操作,结合实例形式分析了javascript使用Blob对象下载文件相关原理、操作技巧与注意事项,需要的朋友可以参考下
recommend-type

【长春理工大学】面向对象程序设计下期末复习浏览题.pdf

这个是我本人在大一下学期期间整理的C++题库,涵盖机考(2018级及以后的南区软件工大一下学期程面向对象程序设计期末考试题库)中几乎所有的题目,并配有解析,方便记忆,考试这个东西……最主要还是自己会,我自己...
recommend-type

linux系统中c++写日志文件功能分享

主要介绍了linux系统中c++写日志文件功能,简化了glog,只保留了写日志文件的功能,只是改写了linux版本,需要的朋友可以参考下
recommend-type

C++ 学生成绩管理系统设计报告书

(3)学生基本信息的读取和保存输入数据等功能 (4)学生基本信息的查询与修改。可以对已有的学生信息进行修改。 (5)学生基本信息的删除。 (6)对该班各科成绩进行分析;对学生成绩进行统计(包括最高分,最低分,排序,...
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

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

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