Implementing a Grade Statistics System with C++ Classes and Inheritance

GradeCalc Class Using Composition

Header File (GradeCalc.hpp)

#pragma once
#include <vector>
#include <array>
#include <string>

class GradeCalc {
public:
    explicit GradeCalc(const std::string& courseName);
    void addGrades(int count);
    void displayGrades() const;
    void sortGrades(bool ascending = false);
    int getMinimum() const;
    int getMaximum() const;
    double calculateAverage() const;
    void displayStatistics();

private:
    void updateStatistics();
    
    std::string courseName;
    std::vector<int> gradeList;
    std::array<int, 5> segmentCounts;
    std::array<double, 5> segmentPercentages;
    bool needsUpdate;
};

Implementation File (GradeCalc.cpp)

#include "GradeCalc.hpp"
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <iostream>

GradeCalc::GradeCalc(const std::string& courseName) 
    : courseName{courseName}, needsUpdate{true} {
    segmentCounts.fill(0);
    segmentPercentages.fill(0.0);
}

void GradeCalc::addGrades(int count) {
    if (count < 0) {
        std::cerr << "Error: Count cannot be negative\n";
        return;
    }
    
    gradeList.reserve(count);
    int score;
    
    for (int i = 0; i < count;) {
        std::cin >> score;
        if (score < 0 || score > 100) {
            std::cerr << "Invalid score. Must be between 0-100\n";
            continue;
        }
        gradeList.push_back(score);
        ++i;
    }
    needsUpdate = true;
}

void GradeCalc::displayGrades() const {
    for (const auto& score : gradeList) {
        std::cout << score << ' ';
    }
    std::cout << '\n';
}

void GradeCalc::sortGrades(bool ascending) {
    if (ascending) {
        std::sort(gradeList.begin(), gradeList.end());
    } else {
        std::sort(gradeList.begin(), gradeList.end(), std::greater<int>());
    }
}

int GradeCalc::getMinimum() const {
    if (gradeList.empty()) return -1;
    return *std::min_element(gradeList.begin(), gradeList.end());
}

int GradeCalc::getMaximum() const {
    if (gradeList.empty()) return -1;
    return *std::max_element(gradeList.begin(), gradeList.end());
}

double GradeCalc::calculateAverage() const {
    if (gradeList.empty()) return 0.0;
    return std::accumulate(gradeList.begin(), gradeList.end(), 0.0) / gradeList.size();
}

void GradeCalc::displayStatistics() {
    if (needsUpdate) updateStatistics();
    
    std::cout << "Course: " << courseName << '\n';
    std::cout << "Average: " << std::fixed << std::setprecision(2) << calculateAverage() << '\n';
    std::cout << "Highest: " << getMaximum() << '\n';
    std::cout << "Lowest: " << getMinimum() << '\n';
    
    const std::array<std::string, 5> ranges = {
        "[0,60)", "[60,70)", "[70,80)", "[80,90)", "[90,100]"
    };
    
    for (int i = ranges.size() - 1; i >= 0; --i) {
        std::cout << ranges[i] << "\t: " << segmentCounts[i] 
                  << " students\t" << segmentPercentages[i] * 100 << "%\n";
    }
}

void GradeCalc::updateStatistics() {
    if (gradeList.empty()) return;
    
    segmentCounts.fill(0);
    segmentPercentages.fill(0.0);
    
    for (int score : gradeList) {
        if (score < 60) segmentCounts[0]++;
        else if (score < 70) segmentCounts[1]++;
        else if (score < 80) segmentCounts[2]++;
        else if (score < 90) segmentCounts[3]++;
        else segmentCounts[4]++;
    }
    
    for (size_t i = 0; i < segmentPercentages.size(); ++i) {
        segmentPercentages[i] = static_cast<double>(segmentCounts[i]) / gradeList.size();
    }
    
    needsUpdate = false;
}

Inheritance-Based GradeCalc Implementation

Modified Header File

#pragma once
#include <vector>
#include <array>
#include <string>

class GradeCalc : private std::vector<int> {
public:
    explicit GradeCalc(const std::string& courseName);
    void addGrades(int count);
    void displayGrades() const;
    void sortGrades(bool ascending = false);
    int getMinimum() const;
    int getMaximum() const;
    double calculateAverage() const;
    void displayStatistics();

private:
    void updateStatistics();
    
    std::string courseName;
    std::array<int, 5> segmentCounts;
    std::array<double, 5> segmentPercentages;
    bool needsUpdate;
};

Graphics System Using Polymorphims

Graph Class Hierarchy

#pragma once
#include <vector>
#include <string>

class Shape {
public:
    virtual void draw() = 0;
    virtual ~Shape() = default;
};

class Circle : public Shape {
public:
    void draw() override;
};

class Triangle : public Shape {
public:
    void draw() override;
};

class Rectangle : public Shape {
public:
    void draw() override;
};

class Canvas {
public:
    void addShape(const std::string& shapeType);
    void renderAll() const;
    ~Canvas();

private:
    std::vector<Shape*> shapes;
};

Implementation

#include "Graph.hpp"
#include <iostream>

void Circle::draw() { std::cout << "Drawing circle\n"; }
void Triangle::draw() { std::cout << "Drawing triangle\n"; }
void Rectangle::draw() { std::cout << "Drawing rectangle\n"; }

void Canvas::addShape(const std::string& shapeType) {
    Shape* shape = nullptr;
    if (shapeType == "circle") shape = new Circle();
    else if (shapeType == "triangle") shape = new Triangle();
    else if (shapeType == "rectangle") shape = new Rectangle();
    
    if (shape) shapes.push_back(shape);
}

void Canvas::renderAll() const {
    for (const auto& shape : shapes) {
        shape->draw();
    }
}

Canvas::~Canvas() {
    for (auto shape : shapes) {
        delete shape;
    }
}

Toy Factory Management System

Toy Class Implementation

class Toy {
public:
    std::string name;
    std::string category;
    std::string color;
    
    void displayInfo() const;
    void setProperties(const std::string& n, const std::string& cat, const std::string& col);
};

class ToyFactory {
private:
    std::vector<Toy> inventory;
    
public:
    void addToy(const Toy& toy);
    void showInventory() const;
    void demonstrateFeatures() const;
};

Tags: C++ Object-Oriented Programming Class Design Inheritance Polymorphism

Posted on Fri, 08 May 2026 14:38:38 +0000 by padanaram