Recap - Generic Container
Part of the Object Oriented Programming section of Coddy's C++ journey — lesson 69 of 104.
Challenge
EasyLet'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. YourGenericContainershould: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 indexgetSize()— returns the current number of elements
Add a function template called
printContainerthat takes aGenericContainer<T>&and prints all elements separated by spaces, followed by a newline.Create a template specialization of
printContainerforGenericContainer<bool>that printstrueorfalseinstead of1or0.Add a variadic function template called
pushAllthat takes aGenericContainer<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):- First integer
- Second integer
- Third integer
- A double value
- A boolean as string (
trueorfalse)
Demonstrate your container by:
- Creating a
GenericContainer<int>with capacity 10 - Using
pushAllto add all three integers at once - Printing the size:
Int container size: <size> - Calling
printContainerto display the integers - Creating a
GenericContainer<double>with capacity 5 - Pushing the double value and the literal
2.5using individualpushcalls - Calling
printContainerto display the doubles - Creating a
GenericContainer<bool>with capacity 5 - Pushing the boolean input and the literal
false - Calling
printContainerto display the booleans (should showtrue/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
1Fundamentals of OOP
External FilesC++ Build & CompilationHeader Files & Source FilesNamespaces & ScopeIntroduction to OOP in C++Classes vs ObjectsThe 'this' PointerMethods (Member Functions)Attributes (Data Members)Ctors & Dtors BasicsRecap - Simple Calculator4Class Properties
Instance vs Static MembersGetters and SettersConst Member FunctionsMutable KeywordStatic Methods and VariablesFriend Functions & ClassesRecap - Bank Account Manager7Inheritance
Basic InheritanceInheritance Access LevelsCtor & Dtor Call OrderMethod OverridingVirtual Functions & VTableMultiple InheritanceVirtual InheritanceRecap - Employee Hierarchy2Memory Management
Stack vs Heap MemoryPointers and ReferencesDynamic Memory (new/delete)Smart Pointers in C++RAII in C++Recap - Dynamic Array Manager5Encapsulation
Access Specifiers in C++Access Specifiers In DepthInformation HidingStruct vs ClassNested & Inner ClassesRecap - Student Records System8Polymorphism
Compile vs Runtime PolymorphFunction OverloadingVirtual Functions RevisitedPure Virtual FunctionsAbstract ClassesInterface Design in C++Dynamic Casting & RTTIRecap - Shape Calculator3Constructors & Destructors
Default ConstructorParameterized ConstructorCopy ConstructorMove ConstructorConstructor Init ListsDelegating ConstructorsDestructor Deep DiveRule of Three / Five / ZeroRecap - String Class6Operator Overloading
Intro to Operator OverloadArithmetic Operator OverloadComparison Operator OverloadStream OperatorsAssignment Operator Overload[] and () Operator OverloadType Conversion OperatorsRecap - Matrix Class9Templates
Function TemplatesClass TemplatesTemplate SpecializationVariadic TemplatesSFINAE & Type Traits BasicsRecap - Generic Container