Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    The STL and professional C++ · Lesson 21 of 21

    Project: a grade book

    Classes, vectors, maps, algorithms and exceptions in one program.

    15 minutes3 games

    We'll build a small grade book for a SeaCode course that brings together everything we've learned. Read and run the code first, then complete the three tasks below, each building on the last.

    C++
    #include <algorithm>
    #include <iostream>
    #include <map>
    #include <stdexcept>
    #include <string>
    #include <vector>
    
    class GradeBook {
    public:
        void add(const std::string& student, int score) {
            if (score < 0 || score > 100) throw std::out_of_range("score must be 0-100");
            scores_[student].push_back(score);
        }
    
        double average(const std::string& student) const {
            auto it = scores_.find(student);
            if (it == scores_.end()) throw std::invalid_argument("unknown student " + student);
            const std::vector<int>& s = it->second;
            int sum = 0;
            for (int x : s) sum += x;
            return static_cast<double>(sum) / s.size();
        }
    
        std::vector<std::string> students() const {
            std::vector<std::string> names;
            for (const auto& entry : scores_) names.push_back(entry.first);
            return names;
        }
    
    private:
        std::map<std::string, std::vector<int>> scores_;
    };
    
    int main() {
        GradeBook book;
        book.add("Laila", 90);
        book.add("Laila", 80);
        book.add("Omar", 70);
        book.add("Nour", 100);
        book.add("Nour", 95);
    
        for (const std::string& name : book.students()) {
            std::cout << name << ": " << book.average(name) << "\n";
        }
        try {
            book.add("Omar", 120);
        } catch (const std::exception& e) {
            std::cout << "rejected: " << e.what() << "\n";
        }
        return 0;
    }
    • GradeBook
      • scores_: map<string, vector<int>>
        • "Laila" → [90, 80]
        • "Nour" → [100, 95]
        • "Omar" → [70]
      • add() → validates, then push_back
      • average() const → throws if unknown

    Task 1: ranking students

    Exercise

    Add a ranking() method (const) returning student names sorted by average, highest first. Print them on one line separated by spaces.

    Expected output: Nour Laila Omar

    C++
    #include <algorithm>
    #include <iostream>
    #include <map>
    #include <string>
    #include <vector>
    
    class GradeBook {
    public:
        void add(const std::string& student, int score) { scores_[student].push_back(score); }
        double average(const std::string& student) const {
            const std::vector<int>& s = scores_.at(student);
            int sum = 0;
            for (int x : s) sum += x;
            return static_cast<double>(sum) / s.size();
        }
        std::vector<std::string> ranking() const {
            std::vector<std::string> names;
            // fill and sort names here
            return names;
        }
    private:
        std::map<std::string, std::vector<int>> scores_;
    };
    
    int main() {
        GradeBook book;
        book.add("Laila", 90); book.add("Laila", 80);
        book.add("Omar", 70);
        book.add("Nour", 100); book.add("Nour", 95);
        for (const std::string& n : book.ranking()) std::cout << n << " ";
        std::cout << "\n";
        return 0;
    }

    Task 2: letter grades

    Exercise

    Write char letter(double avg): 90 and up is A, 80 is B, 70 is C, anything lower F. For each student (alphabetically) print name letter.

    Expected output:
    Ali C
    Laila B
    Nour A
    Omar F
    C++
    #include <iostream>
    #include <map>
    #include <string>
    #include <vector>
    
    char letter(double avg) {
        return '?';
    }
    
    int main() {
        std::map<std::string, std::vector<int>> scores = {
            {"Laila", {90, 80}}, {"Omar", {70, 60}}, {"Nour", {100, 95}}, {"Ali", {72, 78}}
        };
        for (const auto& entry : scores) {
            int sum = 0;
            for (int x : entry.second) sum += x;
            double avg = static_cast<double>(sum) / entry.second.size();
            std::cout << entry.first << " " << letter(avg) << "\n";
        }
        return 0;
    }

    Task 3: course statistics

    Exercise

    From all scores {90, 80, 70, 100, 95, 55}: print the highest, the lowest, and how many are 60 or more, on one line as max 100 min 55 passed 5. Use the algorithms.

    Expected output: max 100 min 55 passed 5

    C++
    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> all = {90, 80, 70, 100, 95, 55};
        
        return 0;
    }

    Important

    Notice the design choices: data is private, read-only methods are const, validation lives in one place (add), errors are clear exceptions, and there's no new or delete anywhere, so no leaks are possible. That's what modern C++ looks like.

    Interview question

    If this GradeBook held a million students and the ranking were requested often, how would you improve it?

    Learn more from

    • cppreference: std::max_elementen.cppreference.com
    • cppreference: std::count_ifen.cppreference.com
    • C++ Core Guidelines: Philosophyisocpp.github.io

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousRecursion🎉 خلّصت C++!
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.