Dynamic Stack  «Prev  Next»

Adding a Constructor to ch_stack (Exercise)

Objective: Add a constructor to the class ch_stack based on a supplied function prototype.

What you are building

In this module you’ve been constructing a dynamically sized stack. In earlier lessons you added constructors for capacity and copying. In this exercise, you’ll add a string-initializing constructor so a stack can be created directly from a C-string.

Prototype to implement

Use the following prototype to add a constructor for ch_stack:

ch_stack::ch_stack(const char* c);

Expected behavior

Implementation notes (C++23 best practices)

Design note: inheritance pitfalls

This module focuses on class construction and ownership, but design still matters. A common OO mistake is choosing inheritance where composition is required. For example, a Stack is not generally a subtype of a List if the list interface allows operations (like inserting in the middle) that violate stack semantics.



  1. A Stack is not a kind-of List (if List allows insertion in the middle and Stack does not).
  2. A ListOfApples is not a kind-of ListOfFruit (if any Fruit can be added to ListOfFruit).

The following relationships may or may not be valid depending on the contracts involved:

  1. A Circle may not be a kind-of Ellipse.
  2. An Ostrich may not be a kind-of Bird.
  3. An Integer may not be a kind-of RationalNumber.

Submit your solution


Next, the exercise result page will display your submission and (optionally) provide guidance on the constructor’s correctness and edge cases (such as nullptr input and capacity limits).