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 <iostream>
#include <string>
class Student {
public:
int id;
std::string name;
};
int main() {
Student s{1, "Alex"};
std::cout << s.id << " " << s.name << std::endl;
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 <iostream>
#include <vector>
#include <string>
class Student {
public:
int id;
std::string name;
};
int main() {
std::vector<Student> students = {{1, "Alex"}, {2, "Sam"}};
std::cout << students.size() << std::endl;
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 <iostream>
#include <vector>
#include <string>
class Student {
public:
int id;
std::string name;
};
int main() {
std::vector<Student> students = {{1, "Alex"}, {2, "Sam"}};
for (const auto &s : students) {
if (s.id == 2) std::cout << s.name << std::endl;
}
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 <iostream>
#include <vector>
class Student {
public:
int id;
int grade;
};
int main() {
std::vector<Student> students = {{1, 80}, {2, 90}};
for (auto &s : students) {
if (s.id == 1) s.grade = 95;
}
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 <iostream>
#include <vector>
#include <algorithm>
class Student {
public:
int id;
};
int main() {
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);
std::cout << students.size() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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