Menu
Coddy logo textTech

Recap - Generic Container

Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 69 of 104.

challenge icon

Challenge

Easy

Let's build a GenericContainer class template that brings together everything you've learned about templates in this chapter. You'll create a flexible, type-safe container that can store elements dynamically and provide useful operations through template techniques.

You'll organize your code across two files:

  • GenericContainer.h: Define your class template here. Your GenericContainer should:

    Store elements dynamically using a pointer to an array, along with tracking the current size and capacity.

    Provide these core methods:

    • A constructor that takes an initial capacity
    • A destructor that properly cleans up the dynamic memory
    • push(T value) — adds an element to the container (assume capacity is always sufficient for this challenge)
    • get(size_t index) — returns the element at the given index
    • getSize() — returns the current number of elements

    Add a function template called printContainer that takes a GenericContainer<T>& and prints all elements separated by spaces, followed by a newline.

    Create a template specialization of printContainer for GenericContainer<bool> that prints true or false instead of 1 or 0.

    Add a variadic function template called pushAll that takes a GenericContainer<T>& and any number of values, pushing each one into the container using a fold expression.

  • main.cpp: Read five inputs (each on a separate line):
    1. First integer
    2. Second integer
    3. Third integer
    4. A double value
    5. A boolean as string (true or false)

    Demonstrate your container by:

    1. Creating a GenericContainer<int> with capacity 10
    2. Using pushAll to add all three integers at once
    3. Printing the size: Int container size: <size>
    4. Calling printContainer to display the integers
    5. Creating a GenericContainer<double> with capacity 5
    6. Pushing the double value and the literal 2.5 using individual push calls
    7. Calling printContainer to display the doubles
    8. Creating a GenericContainer<bool> with capacity 5
    9. Pushing the boolean input and the literal false
    10. Calling printContainer to display the booleans (should show true/false)

For example, with inputs 10, 20, 30, 3.14, and true:

Int container size: 3
10 20 30 
3.14 2.5 
true false 

This challenge combines class templates for the container, template specialization for custom bool printing, and variadic templates for the flexible pushAll function. Your container manages its own memory following RAII principles, demonstrating how templates integrate with proper resource management.

Try it yourself

#include <iostream>
#include <string>
#include "GenericContainer.h"

using namespace std;

int main() {
    // Read inputs
    int num1, num2, num3;
    double doubleVal;
    string boolStr;
    
    cin >> num1;
    cin >> num2;
    cin >> num3;
    cin >> doubleVal;
    cin >> boolStr;
    
    // Convert string to bool
    bool boolVal = (boolStr == "true");
    
    // TODO: Create GenericContainer<int> with capacity 10
    
    // TODO: Use pushAll to add all three integers at once
    
    // TODO: Print the size: "Int container size: <size>"
    
    // TODO: Call printContainer to display the integers
    
    // TODO: Create GenericContainer<double> with capacity 5
    
    // TODO: Push the double value and 2.5 using individual push calls
    
    // TODO: Call printContainer to display the doubles
    
    // TODO: Create GenericContainer<bool> with capacity 5
    
    // TODO: Push the boolean input and false
    
    // TODO: Call printContainer to display the booleans
    
    return 0;
}

All lessons in Object Oriented Programming