Implementing a Student Management System in C++

Implementing a Student Management System in C++

In this article, we will learn how to write a simple student management system using C++. This system can help us manage basic information about students, including their names, student IDs, and grades. Through this example, we will understand the basic concepts of object-oriented programming and master some core concepts of C++.

1. System Functional Requirements

Our student management system needs to implement the following basic functions:

  • Add a student
  • View all student information
  • Find a student’s information by student ID
  • Delete a student’s information

2. Program Structure Design

To facilitate management, we first design a <span>Student</span> class to represent each student’s information:

class Student {public:    string name;        // Name    int id;            // Student ID    float score;       // Score    Student(string _name, int _id, float _score)         : name(_name), id(_id), score(_score) {}};

Then, we need a <span>StudentManager</span> class to manage multiple <span>Student</span> objects:

class StudentManager {private:    vector<Student> students;  // Store all student informationpublic:    void addStudent(const string& name, int id, float score);    void viewStudents() const;    void findStudent(int id) const;    void deleteStudent(int id);};

3. Function Implementation

Below are the specific implementations of each functional function.

Add New Student

void StudentManager::addStudent(const string& name, int id, float score) {    students.push_back(Student(name, id, score));}

View All Student Information

void StudentManager::viewStudents() const {    for (const auto& student : students) {        cout << "Name: " << student.name << ", ID: " << student.id              << ", Score: " << student.score << endl;    }}

Find Specific Student Information by ID

void StudentManager::findStudent(int id) const {   for (const auto& student : students) {       if (student.id == id) {           cout << "Found -> Name: " << student.name                 << ", ID: " << student.id                 << ", Score: " << student.score << endl;           return;       }   }   cout << "No student found with ID: " << id << endl;}

Delete Specific Student Information by ID

void StudentManager::deleteStudent(int id) {   auto it = remove_if(students.begin(), students.end(), [id](const Student& s){       return s.id == id;    });   if(it != students.end()) {         cout << "Deleted -> Name:" << (it->name) << ", ID:" << it->id << endl;        students.erase(it);   } else {        cout << "No student found with ID:" << id << endl;         }}

4. Main Program Entry and Menu Interaction Logic

Finally, in the <span>main</span> function, we create a menu where users can select different operations.

int main() {    StudentManager manager;    while(true){        cout<<"1. Add a new student\n";        cout<<"2. View all students\n";        cout<<"3. Find a student by ID\n";        cout<<"4. Delete a student by ID\n";        cout<<"5. Exit\n";        int choice;        cin >> choice;        switch(choice){            case 1:{                string name;                int id;                float score;                cout<<"Enter Name:";                cin >> name;                cout<<"Enter ID:";                cin >> id;                cout<<"Enter Score:";               cin >> score;               manager.addStudent(name,id,score);            } break;            case 2:               manager.viewStudents();               break;            case 3:{               int idToFind;               cout <<"Enter the student's ID to find:";               cin>>idToFind;              manager.findStudent(idToFind);             } break;             case 4:{                 int deleteId ;                 cout<<"Enter the student's ID to delete:";                 cin >> deleteId ;                  manager.deleteStudent(deleteId);             }break ;             case 5 :                   exit(0);                          break ;                  default :                  cout <<" Invalid option. Please try again!";          }     }     return 0;}

Complete Code Example

Below is the complete code example, which combines the above code segments to form a complete program:

#include <iostream>#include <vector>#include <algorithm>using namespace std;// Student class definition, used to store individual student information.class Student {public:        string name;                int id;                    float score;        // Constructor to initialize member variables.        Student(string _name, int _id, float _score): name(_name), id(_id), score(_score){}// Student management class definition, used for various operations.class StudentManager{private:    vector<Student> students;public:    void addStudent(const string& name, int id, float score);    void viewStudents() const;    void findStudent(int id) const;    void deleteStudent(int id);};void StudentManager::addStudent(const string& name, int id, float score) {    students.push_back(Student(name, id, score));}void StudentManager::viewStudents() const {    for(const auto& student : students) {        cout<<"Name:"<< student.name<<" -> ID:"<< student.id<<" -> Score:"<< student.score<<endl;    }}void StudentManager::findStudent(int id) const {   for(const auto& student : students) {       if(student.id == id) {           cout<<"Found -> Name:"<< student.name<<" -> ID:"<< student.id<<" -> Score:"<< student.score<<endl;           return;       }   }   cout<<"No student found with ID:"<< id<<endl;}void StudentManager::deleteStudent(int id) {   auto it = remove_if(students.begin(), students.end(), [id](const Student& s){       return s.id == id;    });   if(it != students.end()) {         cout<<"Deleted -> Name:"<<(it->name)<<" -> ID:"<<it->id<<endl;        students.erase(it);   } else {        cout<<"No student found with ID:"<<id<<endl;         } }int main() {    StudentManager manager;    while(true){        cout<<"1. Add a new student\n";        cout<<"2. View all students\n";        cout<<"3. Find a student by ID\n";        cout<<"4. Delete a student by ID\n";        cout<<"5. Exit\n";        int choice;        cin >> choice;        switch(choice){            case 1:{                string name;                int id;                float score;                cout<<"Enter Name:";                cin >> name;                cout<<"Enter ID:";                cin >> id;                cout<<"Enter Score:";               cin >> score;               manager.addStudent(name,id,score);            } break;            case 2:               manager.viewStudents();               break;            case 3:{               int idToFind;               cout<<"Enter the student's ID to find:";               cin>>idToFind;              manager.findStudent(idToFind);             } break;             case 4:{                 int deleteId ;                 cout<<"Enter the student's ID to delete:";                 cin >> deleteId ;                  manager.deleteStudent(deleteId);             }break ;             case 5 :                   exit(0);                          break ;                  default :                  cout <<" Invalid option. Please try again!";          }     }     return 0;}

Leave a Comment