C++ Mini Project — Student Management
In this page:
Defining the Student Model
Modeling a student as a class or struct with fields like ID, name, and a collection of grades gives you one well-defined unit to store, pass around, and operate on, instead of tracking parallel arrays of names, IDs, and grades that could drift out of sync.
Example: Defining the Student Model
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
std::string name;
};
// Program execution starts in main()
int main() {
Student s{1, "Alex"};
// Print to the console with cout
std::cout << s.id << " " << s.name << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Student Collection Manager
A std::vector<Student> holds the entire collection of student records in memory, giving you the full toolkit of vector operations -- iterating, searching, sorting -- for free, rather than managing a fixed-size array with manual bounds tracking.
Example: Student Collection Manager
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <string>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
std::string name;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1, "Alex"}, {2, "Sam"}};
// Print to the console with cout
std::cout << students.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Searching Student Records
Searching by ID typically loops through the vector comparing each student's ID field to the target, since IDs are expected to be unique; searching by name may need to handle multiple students sharing the same name and return every match rather than just the first.
Example: Searching Student Records
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <string>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
std::string name;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1, "Alex"}, {2, "Sam"}};
// Range-based for loop: visit every element of students
for (const auto &s : students) {
if (s.id == 2) std::cout << s.name << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Updating Student Grades
Updating a grade means first locating the correct Student object in the collection (usually by ID), then modifying its grade field directly -- since a vector holds actual objects rather than copies elsewhere, this change is immediately reflected everywhere that student is referenced.
Example: Updating Student Grades
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
int grade;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1, 80}, {2, 90}};
// Range-based for loop: visit every element of students
for (auto &s : students) {
if (s.id == 1) s.grade = 95;
}
// Print to the console with cout
std::cout << students[0].grade << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Removing Student Profiles
std::vector::erase(iterator) removes a single element at a given position, shifting every subsequent element down to fill the gap -- combined with a search that first locates the target student's iterator, this is the standard pattern for deleting a specific record.
Example: Removing Student Profiles
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>
// Define the type Student
class Student {
// Members below can be accessed from outside the class
public:
int id;
};
// Program execution starts in main()
int main() {
// Create a vector named students (a resizable array)
std::vector<Student> students = {{1}, {2}, {3}};
auto it = std::find_if(students.begin(), students.end(), [](Student &s) { return s.id == 2; });
if (it != students.end()) students.erase(it);
// Print to the console with cout
std::cout << students.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Not validating the input entered for a student, such as a negative or non-numeric grade.
- Comparing names with different letter case when searching, so a valid record is not found.
- Using
intfor grades that need decimals, which truncates the average.
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced