School Management System Development(C++)

In this article, we will be developing a simple C++ based school management system. The system will manage students, teachers, and courses, allowing for basic operations such as adding, removing, and listing these entities.

Table of Contents

  1. Introduction and Requirements
  2. System Design and Analysis
  3. Implementation
    • 3.1. Base Entity Class
    • 3.2. Student Class
    • 3.3. Teacher Class
    • 3.4. Course Class
    • 3.5. School Class
    • 3.6. Main Function and Menu
  4. Conclusion

1. Introduction and Requirements

We will be developing a basic school management system using C++ that satisfies the following requirements:

  • Manage students, teachers, and courses.
  • Perform basic operations such as adding, removing, and listing students, teachers, and courses.
  • Assign students and teachers to courses.
  • No external libraries or frameworks.

2. System Design and Analysis

To develop the school management system, we will be creating the following classes:

  • BaseEntity: A base class that provides a common interface for entities such as students, teachers, and courses.
  • Student: A derived class representing a student.
  • Teacher: A derived class representing a teacher.
  • Course: A class representing a course, containing students and a teacher.
  • School: A class representing the school, which manages students, teachers, and courses.

Additionally, we will create a main function that provides a menu-driven interface for users to interact with the system.

3. Implementation

3.1. Base Entity Class

The BaseEntity class provides a common interface for entities. It contains a unique ID and a name.

class BaseEntity {
public:
BaseEntity(int id, const std::string& name) : id_(id), name_(name) {}

int getId() const {
return id_;
}

const std::string& getName() const {
return name_;
}

void setName(const std::string& name) {
name_ = name;
}

private:
int id_;
std::string name_;
};

3.2. Student Class

The Student class is a derived class of BaseEntity representing a student.

class Student : public BaseEntity {
public:
Student(int id, const std::string& name) : BaseEntity(id, name) {}
};

3.3. Teacher Class

The Teacher class is a derived class of BaseEntity representing a teacher.

class Teacher : public BaseEntity {
public:
Teacher(int id, const std::string& name) : BaseEntity(id, name) {}
};

3.4. Course Class

The Course class represents a course, which contains a list of enrolled students and a teacher.

class Course : public BaseEntity {
public:
Course(int id, const std::string& name, Teacher* teacher)
: BaseEntity(id, name), teacher_(teacher) {}
void enrollStudent(Student* student) {
students_.push_back(student);
}

void removeStudent(int studentId) {
students_.erase(std::remove_if(students_.begin(), students_.end(),
[studentId](Student* s) { return s->getId() == studentId; }),
students_.end());
}

const std::vector<Student*>& getStudents() const {
return students_;
}

Teacher* getTeacher() const {
return teacher_;
}

void setTeacher(Teacher* teacher) {
teacher_ = teacher;
}
private:
std::vector<Student*> students_;
Teacher* teacher_;
};

3.5. School Class

The School class represents the school, which manages students, teachers, and courses.

class School {
public:
~School() {
for (auto* student : students_) delete student;
for (auto* teacher : teachers_) delete teacher;
for (auto* course : courses_) delete course;
}

void addStudent(int id, const std::string& name) {
students_.push_back(new Student(id, name));
}

void removeStudent(int id) {
auto it = std::find_if(students_.begin(), students_.end(),
[id](Student* s) { return s->getId() == id; });
if (it != students_.end()) {
delete *it;
students_.erase(it);
}
}

void addTeacher(int id, const std::string& name) {
teachers_.push_back(new Teacher(id, name));
}

void removeTeacher(int id) {
auto it = std::find_if(teachers_.begin(), teachers_.end(),
[id](Teacher* t) { return t->getId() == id; });
if (it != teachers_.end()) {
delete *it;
teachers_.erase(it);
}
}

void addCourse(int id, const std::string& name, int teacherId) {
Teacher* teacher = getTeacherById(teacherId);
if (teacher) {
courses_.push_back(new Course(id, name, teacher));
}
}

void removeCourse(int id) {
auto it = std::find_if(courses_.begin(), courses_.end(),
[id](Course* c) { return c->getId() == id; });
if (it != courses_.end()) {
delete *it;
courses_.erase(it);
}
}

Student* getStudentById(int id) {
auto it = std::find_if(students_.begin(), students_.end(),
[id](Student* s) { return s->getId() == id; });
return (it != students_.end()) ? *it : nullptr;
}

Teacher* getTeacherById(int id) {
auto it = std::find_if(teachers_.begin(), teachers_.end(),
[id](Teacher* t) { return t->getId() == id; });
return (it != teachers_.end()) ? *it : nullptr;
}

Course* getCourseById(int id) {
auto it = std::find_if(courses_.begin(), courses_.end(),
[id](Course* c) { return c->getId() == id; });
return (it != courses_.end()) ? *it : nullptr;
}

const std::vector<Student*>& getStudents() const {
return students_;
}

const std::vector<Teacher*>& getTeachers() const {
return teachers_;
}

const std::vector
<Course*>& getCourses() const {
return courses_;
}

private:
std::vector<Student*> students_;
std::vector<Teacher*> teachers_;
std::vector<Course*> courses_;
};

3.6. Main Function and Menu

The main function provides a menu-driven interface for users to interact with the school management system.

void displayMenu() {
std::cout << "\nSchool Management System\n";
std::cout << "1. Add student\n";
std::cout << "2. Remove student\n";
std::cout << "3. Add teacher\n";
std::cout << "4. Remove teacher\n";
std::cout << "5. Add course\n";
std::cout << "6. Remove course\n";
std::cout << "7. Assign student to course\n";
std::cout << "8. Remove student from course\n";
std::cout << "9. List students\n";
std::cout << "10. List teachers\n";
std::cout << "11. List courses\n";
std::cout << "12. Exit\n";
std::cout << "Enter your choice: ";
}

int main() {
School school;
int choice;
int id;
std::string name;

while (true) {
displayMenu();
std::cin >> choice;

switch (choice) {
case 1: // Add student
// ...
break;
case 2: // Remove student
// ...
break;
case 3: // Add teacher
// ...
break;
case 4: // Remove teacher
// ...
break;
case 5: // Add course
// ...
break;
case 6: // Remove course
// ...
break;
case 7: // Assign student to course
// ...
break;
case 8: // Remove student from course
// ...
break;
case 9: // List students
// ...
break;
case 10: // List teachers
// ...
break;
case 11: // List courses
// ...
break;
case 12: // Exit
return 0;
default:
std::cout << "Invalid choice! Try again.\n";
}
}
}

In this simplified version of the main function, the switch statement structure is outlined for handling user inputs. You will need to complete the implementation by adding the appropriate code to handle each case.

4. Conclusion

In this article, we have developed a simple C++ based school management system that manages students, teachers, and courses. We have covered the thought process, analysis, development process, and provided the final code. This basic system can be further extended and refined to meet the requirements of a more advanced school management system, such as integrating a database for data persistence, adding more functionalities, or improving the user interface.

Here’s the complete code for the simple C++ based school management system.

#include <iostream>
#include <vector>
#include <algorithm>

class BaseEntity {
public:
BaseEntity(int id, const std::string& name) : id_(id), name_(name) {}

int getId() const {
return id_;
}

const std::string& getName() const {
return name_;
}

void setName(const std::string& name) {
name_ = name;
}

private:
int id_;
std::string name_;
};

class Student : public BaseEntity {
public:
Student(int id, const std::string& name) : BaseEntity(id, name) {}
};

class Teacher : public BaseEntity {
public:
Teacher(int id, const std::string& name) : BaseEntity(id, name) {}
};

class Course : public BaseEntity {
public:
Course(int id, const std::string& name, Teacher* teacher)
: BaseEntity(id, name), teacher_(teacher) {}

void enrollStudent(Student* student) {
students_.push_back(student);
}

void removeStudent(int studentId) {
students_.erase(std::remove_if(students_.begin(), students_.end(),
[studentId](Student* s) { return s->getId() == studentId; }),
students_.end());
}

const std::vector<Student*>& getStudents() const {
return students_;
}

Teacher* getTeacher() const {
return teacher_;
}

void setTeacher(Teacher* teacher) {
teacher_ = teacher;
}

private:
std::vector<Student*> students_;
Teacher* teacher_;
};

class School {
public:
~School() {
for (auto* student : students_) delete student;
for (auto* teacher : teachers_) delete teacher;
for (auto* course : courses_) delete course;
}

void addStudent(int id, const std::string& name) {
students_.push_back(new Student(id, name));
}

void removeStudent(int id) {
auto it = std::find_if(students_.begin(), students_.end(),
[id](Student* s) { return s->getId() == id; });
if (it != students_.end()) {
delete *it;
students_.erase(it);
}
}

void addTeacher(int id, const std::string& name) {
teachers_.push_back(new Teacher(id, name));
}

void removeTeacher(int id) {
auto it = std::find_if(teachers_.begin(), teachers_.end(),
[id](Teacher* t) { return t->getId() == id; });
if (it != teachers_.end()) {
delete *it;
teachers_.erase(it);
}
}

void addCourse(int id, const std::string& name, int teacherId) {
Teacher* teacher = getTeacherById(teacherId);
if (teacher) {
courses_.push_back(new Course(id, name, teacher));
}
}

void removeCourse(int id) {
auto it = std::find_if(courses_.begin(), courses_.end(),
[id](Course* c) { return c->getId() == id; });
if (it != courses_.end()) {
delete *it;
courses_.erase(it);
}
}

Student* getStudentById(int id) {
auto it = std::find_if(students_.begin(), students_.end(),
[id](Student* s) { return s->getId() == id; });
return (it != students_.end()) ? *it : nullptr
}

Teacher* getTeacherById(int id) {
auto it = std::find_if(teachers_.begin(), teachers_.end(),
[id](Teacher* t) { return t->getId() == id; });
return (it != teachers_.end()) ? *it : nullptr;
}

Course* getCourseById(int id) {
auto it = std::find_if(courses_.begin(), courses_.end(),
[id](Course* c) { return c->getId() == id; });
return (it != courses_.end()) ? *it : nullptr;
}

const std::vector<Student*>& getStudents() const {
return students_;
}

const std::vector<Teacher*>& getTeachers() const {
return teachers_;
}

const std::vector<Course*>& getCourses() const {
return courses_;
}
private:
std::vector<Student*> students_;
std::vector<Teacher*> teachers_;
std::vector<Course*> courses_;
};

void displayMenu() {
std::cout << "\nSchool Management System\n";
std::cout << "1. Add student\n";
std::cout << "2. Remove student\n";
std::cout << "3. Add teacher\n";
std::cout << "4. Remove teacher\n";
std::cout << "5. Add course\n";
std::cout << "6. Remove course\n";
std::cout << "7. Assign student to course\n";
std::cout << "8. Remove student from course\n";
std::cout << "9. List students\n";
std::cout << "10. List teachers\n";
std::cout << "11. List courses\n";
std::cout << "12. Exit\n";
std::cout << "Enter your choice: ";
}

int main() {
School school;
int choice;
int id;
int teacherId;
int courseId;
std::string name;
while (true) {
displayMenu();
std::cin >> choice;

switch (choice) {
case 1: // Add student
std::cout << "Enter student ID and name: ";
std::cin >> id >> name;
school.addStudent(id, name);
break;
case 2: // Remove student
std::cout << "Enter student ID to remove: ";
std::cin >> id;
school.removeStudent(id);
break;
case 3: // Add teacher
std::cout << "Enter teacher ID and name: ";
std::cin >> id >> name;
school.addTeacher(id, name);
break;
case 4: // Remove teacher
std::cout << "Enter teacher ID to remove: ";
std::cin >> id;
school.removeTeacher(id);
break;
case 5: // Add course
std::cout << "Enter course ID, name, and teacher ID: ";
std::cin >> id >> name >> teacherId;
school.addCourse(id, name, teacherId);
break;
case 6: // Remove course
std::cout << "Enter course ID to remove: ";
std::cin >> id;
school.removeCourse(id);
break;
case 7: // Assign student to course
std::cout << "Enter student ID and course ID: ";
std::cin >> id >> courseId;
if (Student* student = school.getStudentById(id)) {
if (Course* course = school.getCourseById(courseId)) {
course->enrollStudent(student);
} else {
std::cout << "Course not found.\n";
}
} else {
std::cout << "Student not found.\n";
}
break;
case 8: // Remove student from course
std::cout << "Enter student ID and course ID: ";
std::cin >> id >> courseId;
if (Course* course = school.getCourseById(courseId)) {
course->removeStudent(id);
} else {
std::cout << "Course not found.\n";
}
break;
case 9: // List students
std::cout << "\nStudents:\n";
for (const Student* student : school.getStudents()) {
std::cout << "ID: " << student->getId() << ", Name: " << student->getName() << "\n";
}
break;
case 10: // List teachers
std::cout << "\nTeachers:\n";
for (const Teacher* teacher : school.getTeachers()) {
std::cout << "ID: " << teacher->getId() << ", Name: " << teacher->getName() << "\n";
}
break;
case 11: // List courses
std::cout << "\nCourses:\n";
for (const Course* course : school.getCourses()) {
std::cout << "ID: " << course->getId() << ", Name: " << course->getName() << ", Teacher: " << course->getTeacher()->getName() << "\n";
std::cout << "Students enrolled:\n";
for (const Student* student : course->getStudents()) {
std::cout << " ID: " << student->getId() << ", Name: " << student->getName() << "\n";
}
}
break;
case 12: // Exit
return 0;
default:
std::cout << "Invalid choice! Try again.\n";
}
}
}