← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 16 of 17

C++ Mini Project — Student Management

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.