Project: A Console Grade Book
💻 C++Build a complete console program with classes, vectors and formatted reports.
This project combines everything in the course into a small program you could extend for a real school.
- Model students with a class.
- Store them in a vector.
- Print a sorted report.
Design
Create a Student class with a name and a vector of scores. A GradeBook class holds students and provides add, report and class average functions.
Build the Model
Keep printing out of the model where possible. Methods should calculate and return values so they are easy to test.
#include <numeric>
#include <string>
#include <vector>
class Student {
private:
std::string name;
std::vector<int> scores;
public:
explicit Student(std::string name) : name(std::move(name)) {}
void addScore(int score) {
if (score < 0 || score > 100) {
throw std::invalid_argument("Score must be 0 to 100");
}
scores.push_back(score);
}
double average() const {
if (scores.empty()) return 0;
int total = std::accumulate(scores.begin(), scores.end(), 0);
return static_cast<double>(total) / scores.size();
}
const std::string& getName() const { return name; }
};Report
Sort students by average, then print aligned columns and a class average. Handle a student with no scores without dividing by zero.
Practice
- Add a highestScore method to Student.
- Add a subject field.
- Print a pass or fail status for each student.
Hands-on exercise
Build the grade book.
1. Add at least four students with two or three scores.
2. Reject scores outside 0 to 100.
3. Print a report sorted by average, highest first.
4. Print the class average at the end.
Recap
A finished console program separates the model from output, uses standard containers and algorithms, and handles edge cases.
Example
#include <numeric>
#include <string>
#include <vector>
class Student {
private:
std::string name;
std::vector<int> scores;
public:
explicit Student(std::string name) : name(std::move(name)) {}
void addScore(int score) {
if (score < 0 || score > 100) {
throw std::invalid_argument("Score must be 0 to 100");
}
scores.push_back(score);
}
double average() const {
if (scores.empty()) return 0;
int total = std::accumulate(scores.begin(), scores.end(), 0);
return static_cast<double>(total) / scores.size();
}
const std::string& getName() const { return name; }
};
Exercises
1. Add at least four students with two or three scores.
2. Reject scores outside 0 to 100.
3. Print a report sorted by average, highest first.
4. Print the class average at the end.
Expected Output
A sorted report with aligned averages and a class average.
Lesson Nav
C++ Course
0/9 lessons completed
- 1 Your First C++ Program Tutorial
- 2 Variables, Types and Input Tutorial
- 3 Conditions and Loops Tutorial
- 4 Functions and Parameters Tutorial
- 5 Arrays and Vectors Tutorial
- 6 Pointers and References Tutorial
- 7 Classes and Objects Tutorial
- 8 The STL: Containers and Algorithms Tutorial
- 9 Project: A Console Grade Book Project