cpp

examples

examples.cpp⚙️
/**
 * Class Templates - Examples
 * Compile: g++ -std=c++17 -Wall examples.cpp -o examples
 */

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

// ============================================================
// SECTION 1: BASIC CLASS TEMPLATE
// ============================================================

template<typename T>
class Box {
    T value;
public:
    Box(T v) : value(v) {}
    T get() const { return value; }
    void set(T v) { value = v; }
};

void demoBasic() {
    cout << "--- Basic Class Template ---\n" << endl;
    
    Box<int> intBox(42);
    Box<double> dblBox(3.14);
    Box<string> strBox("Hello");
    
    cout << "  int Box: " << intBox.get() << endl;
    cout << "  double Box: " << dblBox.get() << endl;
    cout << "  string Box: " << strBox.get() << endl;
}

// ============================================================
// SECTION 2: TEMPLATE STACK
// ============================================================

template<typename T>
class Stack {
    vector<T> data;
public:
    void push(const T& value) { data.push_back(value); }
    
    T pop() {
        T top = data.back();
        data.pop_back();
        return top;
    }
    
    T peek() const { return data.back(); }
    bool empty() const { return data.empty(); }
    size_t size() const { return data.size(); }
};

void demoStack() {
    cout << "\n--- Template Stack ---\n" << endl;
    
    Stack<int> nums;
    nums.push(1);
    nums.push(2);
    nums.push(3);
    
    cout << "  Popping: ";
    while (!nums.empty()) {
        cout << nums.pop() << " ";
    }
    cout << endl;
    
    Stack<string> words;
    words.push("Hello");
    words.push("World");
    cout << "  String peek: " << words.peek() << endl;
}

// ============================================================
// SECTION 3: NON-TYPE PARAMETERS
// ============================================================

template<typename T, size_t Size>
class Array {
    T data[Size] = {};
public:
    T& operator[](size_t i) { return data[i]; }
    const T& operator[](size_t i) const { return data[i]; }
    constexpr size_t size() const { return Size; }
    
    void fill(const T& value) {
        for (size_t i = 0; i < Size; ++i) data[i] = value;
    }
};

void demoNonType() {
    cout << "\n--- Non-Type Parameters ---\n" << endl;
    
    Array<int, 5> arr;
    for (size_t i = 0; i < arr.size(); ++i) {
        arr[i] = i * 10;
    }
    
    cout << "  Array: ";
    for (size_t i = 0; i < arr.size(); ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
    
    Array<double, 3> dbls;
    dbls.fill(3.14);
    cout << "  Filled: " << dbls[0] << " " << dbls[1] << " " << dbls[2] << endl;
}

// ============================================================
// SECTION 4: PAIR TEMPLATE
// ============================================================

template<typename T, typename U>
class Pair {
public:
    T first;
    U second;
    
    Pair(T f, U s) : first(f), second(s) {}
};

template<typename T, typename U>
Pair<T, U> makePair(T first, U second) {
    return Pair<T, U>(first, second);
}

void demoPair() {
    cout << "\n--- Pair Template ---\n" << endl;
    
    Pair<string, int> person("Alice", 30);
    cout << "  " << person.first << " is " << person.second << endl;
    
    auto coord = makePair(10.5, 20.5);
    cout << "  Coordinates: (" << coord.first << ", " << coord.second << ")" << endl;
}

// ============================================================
// SECTION 5: STATIC MEMBERS
// ============================================================

template<typename T>
class Counter {
    static int instanceCount;
    T value;
public:
    Counter(T v) : value(v) { ++instanceCount; }
    ~Counter() { --instanceCount; }
    static int getCount() { return instanceCount; }
};

template<typename T>
int Counter<T>::instanceCount = 0;

void demoStatic() {
    cout << "\n--- Static Members ---\n" << endl;
    
    {
        Counter<int> a(1), b(2), c(3);
        cout << "  int counters: " << Counter<int>::getCount() << endl;
        
        Counter<double> x(1.0);
        cout << "  double counters: " << Counter<double>::getCount() << endl;
    }
    
    cout << "  After scope - int: " << Counter<int>::getCount() << endl;
    cout << "  After scope - double: " << Counter<double>::getCount() << endl;
}

// ============================================================
// SECTION 6: TEMPLATE SPECIALIZATION
// ============================================================

// Primary template
template<typename T>
class TypeInfo {
public:
    static string name() { return "unknown"; }
};

// Specializations
template<> class TypeInfo<int> {
public: static string name() { return "int"; }
};

template<> class TypeInfo<double> {
public: static string name() { return "double"; }
};

template<> class TypeInfo<string> {
public: static string name() { return "string"; }
};

// Partial specialization for pointers
template<typename T>
class TypeInfo<T*> {
public:
    static string name() { return "pointer to " + TypeInfo<T>::name(); }
};

void demoSpecialization() {
    cout << "\n--- Template Specialization ---\n" << endl;
    
    cout << "  int: " << TypeInfo<int>::name() << endl;
    cout << "  double: " << TypeInfo<double>::name() << endl;
    cout << "  string: " << TypeInfo<string>::name() << endl;
    cout << "  int*: " << TypeInfo<int*>::name() << endl;
    cout << "  char: " << TypeInfo<char>::name() << endl;
}

// ============================================================
// SECTION 7: LINKED LIST TEMPLATE
// ============================================================

template<typename T>
class LinkedList {
    struct Node {
        T data;
        Node* next;
        Node(T d) : data(d), next(nullptr) {}
    };
    
    Node* head = nullptr;
    
public:
    ~LinkedList() {
        while (head) {
            Node* temp = head;
            head = head->next;
            delete temp;
        }
    }
    
    void push_front(T value) {
        Node* node = new Node(value);
        node->next = head;
        head = node;
    }
    
    void print() const {
        cout << "  List: ";
        for (Node* curr = head; curr; curr = curr->next) {
            cout << curr->data << " -> ";
        }
        cout << "null" << endl;
    }
};

void demoLinkedList() {
    cout << "\n--- Linked List Template ---\n" << endl;
    
    LinkedList<int> list;
    list.push_front(3);
    list.push_front(2);
    list.push_front(1);
    list.print();
    
    LinkedList<string> strList;
    strList.push_front("World");
    strList.push_front("Hello");
    strList.print();
}

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

int main() {
    cout << "╔══════════════════════════════════════╗" << endl;
    cout << "║     CLASS TEMPLATES - EXAMPLES       ║" << endl;
    cout << "╚══════════════════════════════════════╝" << endl;
    
    demoBasic();
    demoStack();
    demoNonType();
    demoPair();
    demoStatic();
    demoSpecialization();
    demoLinkedList();
    
    cout << "\n=== Complete ===" << endl;
    return 0;
}
Examples - C++ Tutorial | DeepML