cpp

exercises

exercises.cpp⚙️
/**
 * Abstract Classes - Exercises
 * Compile: g++ -std=c++17 -Wall exercises.cpp -o exercises
 */

#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;

// ============================================================
// Exercise 1: Basic Abstract Class ⭐
// ============================================================

// TODO: Create abstract class Animal with:
// - pure virtual speak()
// - pure virtual move()
// - concrete breathe() that prints "Breathing..."

// TODO: Create Dog and Bird that implement Animal

void exercise1() {
    // Dog dog; Bird bird;
    // dog.speak(); dog.move(); dog.breathe();
    // bird.speak(); bird.move();
    cout << "(Implement Animal hierarchy)" << endl;
}

// ============================================================
// Exercise 2: Interface Implementation ⭐⭐
// ============================================================

class IComparable {
public:
    virtual int compareTo(const IComparable& other) const = 0;
    virtual ~IComparable() = default;
};

// TODO: Create Student class implementing IComparable
// Compare by GPA (higher GPA = greater)

void exercise2() {
    // Student s1("Alice", 3.8), s2("Bob", 3.5);
    // cout << s1.compareTo(s2) << endl;  // > 0 (Alice has higher GPA)
    cout << "(Implement Student with IComparable)" << endl;
}

// ============================================================
// Exercise 3: Multiple Interfaces ⭐⭐
// ============================================================

class IReadable {
public:
    virtual string read() const = 0;
    virtual ~IReadable() = default;
};

class IWritable {
public:
    virtual void write(const string& data) = 0;
    virtual ~IWritable() = default;
};

// TODO: Create File class implementing both interfaces

void exercise3() {
    // File f;
    // f.write("Hello");
    // cout << f.read() << endl;
    cout << "(Implement File with IReadable and IWritable)" << endl;
}

// ============================================================
// Exercise 4: Template Method Pattern ⭐⭐⭐
// ============================================================

class DataProcessor {
public:
    // Template method - final algorithm
    void process() {
        loadData();
        processData();
        saveData();
    }
    
    virtual ~DataProcessor() = default;
    
protected:
    virtual void loadData() = 0;
    virtual void processData() = 0;
    virtual void saveData() = 0;
};

// TODO: Create CSVProcessor and JSONProcessor

void exercise4() {
    // CSVProcessor csv;
    // csv.process();  // Loads CSV, processes, saves
    cout << "(Implement DataProcessor subclasses)" << endl;
}

// ============================================================
// Exercise 5: Abstract with Partial Implementation ⭐⭐
// ============================================================

class Calculator {
public:
    virtual double calculate(double a, double b) = 0;
    
    // Provided implementations using calculate()
    double calculateAndRound(double a, double b) {
        return static_cast<int>(calculate(a, b) + 0.5);
    }
    
    virtual ~Calculator() = default;
};

// TODO: Create Add, Subtract, Multiply, Divide calculators

void exercise5() {
    // Add add; Multiply mul;
    // cout << add.calculate(5, 3) << endl;  // 8
    // cout << mul.calculateAndRound(2.5, 3.5) << endl;  // 9
    cout << "(Implement Calculator subclasses)" << endl;
}

// ============================================================
// Exercise 6: Plugin System ⭐⭐⭐
// ============================================================

class IPlugin {
public:
    virtual string getName() const = 0;
    virtual void execute() = 0;
    virtual ~IPlugin() = default;
};

// TODO: Create at least 2 plugins
// TODO: Create PluginManager that stores and runs all plugins

void exercise6() {
    // PluginManager pm;
    // pm.addPlugin(make_unique<LoggerPlugin>());
    // pm.addPlugin(make_unique<AnalyticsPlugin>());
    // pm.runAll();
    cout << "(Implement plugin system)" << endl;
}

// ============================================================
// Exercise 7: Shape Factory ⭐⭐⭐
// ============================================================

class Shape {
public:
    virtual double area() const = 0;
    virtual string name() const = 0;
    virtual ~Shape() = default;
};

// TODO: Create Circle, Square, Triangle
// TODO: Create ShapeFactory with static create(string type) method

void exercise7() {
    // auto c = ShapeFactory::create("circle", 5);
    // auto s = ShapeFactory::create("square", 4);
    // cout << c->name() << " area: " << c->area() << endl;
    cout << "(Implement ShapeFactory)" << endl;
}

// ============================================================
// MAIN
// ============================================================

int main() {
    cout << "=== Abstract Classes Exercises ===" << endl;
    
    cout << "\nEx1: Basic Abstract" << endl;
    exercise1();
    
    cout << "\nEx2: Interface" << endl;
    exercise2();
    
    cout << "\nEx3: Multiple Interfaces" << endl;
    exercise3();
    
    cout << "\nEx4: Template Method" << endl;
    exercise4();
    
    cout << "\nEx5: Partial Implementation" << endl;
    exercise5();
    
    cout << "\nEx6: Plugin System" << endl;
    exercise6();
    
    cout << "\nEx7: Shape Factory" << endl;
    exercise7();
    
    return 0;
}

// ============================================================
// ANSWERS
// ============================================================
/*
Ex1:
class Animal {
public:
    virtual void speak() = 0;
    virtual void move() = 0;
    void breathe() { cout << "Breathing..." << endl; }
    virtual ~Animal() = default;
};
class Dog : public Animal {
    void speak() override { cout << "Woof!" << endl; }
    void move() override { cout << "Running" << endl; }
};

Ex2:
class Student : public IComparable {
    string name; double gpa;
public:
    Student(string n, double g) : name(n), gpa(g) {}
    int compareTo(const IComparable& other) const override {
        const Student& s = dynamic_cast<const Student&>(other);
        if (gpa > s.gpa) return 1;
        if (gpa < s.gpa) return -1;
        return 0;
    }
};

Ex3:
class File : public IReadable, public IWritable {
    string content;
public:
    string read() const override { return content; }
    void write(const string& data) override { content = data; }
};

Ex5:
class Add : public Calculator {
    double calculate(double a, double b) override { return a + b; }
};
class Multiply : public Calculator {
    double calculate(double a, double b) override { return a * b; }
};
*/
Exercises - C++ Tutorial | DeepML