Skip to main content

Lesson 6 of 7

Practice & Quiz - C++ Mastery Review

A capstone review quiz covering all 11 modules: OOP, memory management, templates, STL, modern C++, error handling, and the Rule of 5. 5 questions that integrate concepts across modules.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Mini Challenge - Extend the IMS with a Discount System

Mini Challenge - Extend the IMS with a Discount System
#include <iostream>
#include <vector>
#include <memory>
#include <string>
#include <functional>
#include <algorithm>
#include <optional>
#include <stdexcept>

// A discount strategy applied to prices - demonstrates:
// - polymorphism (virtual apply)
// - lambdas as strategy
// - optional for "no discount"
// - templates for discount registry

class DiscountStrategy {
public:
    virtual ~DiscountStrategy() = default;
    virtual double apply(double price) const = 0;
    virtual std::string name()          const = 0;
};

class PercentDiscount final : public DiscountStrategy {
    double pct_;
public:
    explicit PercentDiscount(double pct) : pct_(pct) {
        if (pct < 0 || pct > 100) throw std::invalid_argument("Discount % must be 0-100");
    }
    double      apply(double p) const override { return p * (1.0 - pct_ / 100.0); }
    std::string name()          const override { return std::to_string((int)pct_) + "% off"; }
};

class FixedDiscount final : public DiscountStrategy {
    double amount_;
public:
    explicit FixedDiscount(double amount) : amount_(amount) {}
    double      apply(double p) const override { return std::max(0.0, p - amount_); }
    std::string name()          const override { return "$" + std::to_string(amount_) + " off"; }
};

// Lambda-based discount (type-erased)
class LambdaDiscount final : public DiscountStrategy {
    std::function<double(double)> fn_;
    std::string name_;
public:
    LambdaDiscount(std::string name, std::function<double(double)> fn)
        : fn_(std::move(fn)), name_(std::move(name)) {}
    double      apply(double p) const override { return fn_(p); }
    std::string name()          const override { return name_; }
};

// Template registry - stores any DiscountStrategy
template<typename Key = std::string>
class DiscountRegistry {
    std::unordered_map<Key, std::unique_ptr<DiscountStrategy>> table_;
public:
    void add(Key key, std::unique_ptr<DiscountStrategy> d) {
        table_[std::move(key)] = std::move(d);
    }
    std::optional<double> apply(const Key& key, double price) const {
        auto it = table_.find(key);
        if (it == table_.end()) return std::nullopt;
        return it->second->apply(price);
    }
    void list() const {
        for (const auto& [k, d] : table_)
            std::cout << "  " << k << " -> " << d->name() << "\n";
    }
};

int main() {
    DiscountRegistry<> reg;
    reg.add("vip",       std::make_unique<PercentDiscount>(20.0));
    reg.add("clearance", std::make_unique<FixedDiscount>(10.0));
    reg.add("bogo",      std::make_unique<LambdaDiscount>(
        "buy 2 get 1 half off",
        [](double p){ return p * 0.75; }));

    reg.list();
    std::cout << "\n";

    double price = 49.99;
    for (const auto& code : {"vip", "clearance", "bogo", "unknown"}) {
        auto result = reg.apply(code, price);
        if (result)
            std::cout << code << ": $" << price << " -> $" << *result << "\n";
        else
            std::cout << code << ": no such discount\n";
    }
    return 0;
}

Capstone Quiz

Capstone Quiz
Question 1 of 5

The IMS uses `vector<unique_ptr<Product>>` for main storage and `unordered_map<int,Product*>` for ID lookup. The raw pointer in the map is a non-owning observer. What happens if you erase from the vector without updating the map?

Tip

Tip

Practice Practice Quiz C Mastery Review in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

From Fundamentals to Modern C++20, STL Architecture, RAII, Metaprogramming & Capstone Delivery

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz C Mastery Review from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz C Mastery Review is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Topics in This Module

0/7 done

Final Projects

Congratulations on completing the course! Apply everything you've learned by building one of these real-world projects.

Project 1: Game Engine Basics

Advanced

Build a simple 2D game engine with entities, rendering, and collision detection.

C++SFMLOOP
Key features
Entity system Collision detection Scene management Resource loading
22-30 hoursGoal: A working game engine foundation.

Project 2: Custom STL Containers

Advanced

Implement your own versions of STL containers (vector, map, list) with iterators.

C++TemplatesIterators
Key features
Generic containers Iterator support Exception safety Performance benchmarks
18-24 hoursGoal: Deep C++ mastery.

Project 3: Multi-threaded Download Manager

Advanced

Build a download manager with concurrent downloads and progress tracking.

C++17ThreadsNetworking
Key features
Concurrent downloads Progress tracking Pause/resume Retry logic
16-22 hoursGoal: Real-world multithreading experience.