std::stack/std::vector.!s.empty() over s.size() > 0, and show idiomatic I/O and loop structure.In this module you built a dynamically sized stack by modeling the stack as a C++ class and then refining that class across several focused lessons. The workflow pages you completed form a practical progression:
push, pop, top, empty, size).Put simply: you learned to treat a stack not just as a data structure, but as an object with invariants and ownership. The class encapsulates memory allocation, stack state, and safe copying so clients can use it correctly without knowing the internal details.
top represents, what “empty” means).
new[], your class must also delete[] and must define safe copying.
This module intentionally exposes manual memory management because it teaches why constructors, copy constructors, and destructors exist. In production C++23, the most common best practice is to avoid raw owning pointers entirely by storing stack elements in RAII types:
std::vector<T> for contiguous storage (often fastest in practice)std::deque<T> (often used under std::stack)std::string for character buffers when “string behavior” is intendedWhen your class is composed of RAII members, the compiler-generated copy/move/destructor operations are typically correct automatically. That’s the Rule of Zero, and it dramatically reduces the chance of leaks and double frees.
If you simply need a stack container, prefer std::stack (an adaptor) or a vector-backed approach:
#include <stack>
#include <string>
#include <iostream>
int main() {
std::stack<std::string> s;
s.push("Tom");
s.push("Dick");
s.push("Harry");
while (!s.empty()) {
std::cout << s.top() << "\n";
s.pop();
}
}
If you want to extend your ch_stack design further, C++23 offers a few natural upgrades:
std::string_view (keep the original const char* constructor for compatibility).char* to std::vector<char> and delete the manual destructor (Rule of Zero).Completing this module means you can now read, design, and maintain class-based data structures with a sharper understanding of lifetime, copying, and resource ownership—skills that transfer directly to real systems code.