Menu
Coddy logo textTech

Recap: Secret Box

Part of the Object Oriented Programming section of Coddy's C journey — lesson 21 of 61.

challenge icon

Challenge

Easy

Let's build a Box module that demonstrates complete encapsulation. Your box will have three hidden dimensions—width, height, and depth—that can only be accessed through the functions you provide. This is the full opaque pointer pattern in action!

You'll create three files:

  • box.h: Your public interface. Declare an opaque Box type using only a forward declaration—the struct body stays hidden. Declare these functions:
    • create_box — takes three integers (width, height, depth) and returns a Box pointer
    • destroy_box — frees the allocated memory
    • get_volume — takes a const Box * and returns the volume (width × height × depth)
    • set_dimensions — takes a Box pointer and three new dimension values, returns int (1 for success, 0 if any dimension is negative or zero)
    Use include guards with the symbol BOX_H.
  • box.c: Define the actual struct Box with three hidden members: int width, int height, and int depth. Implement all four functions. Your constructor should allocate and initialize the box. The set_dimensions function must validate that all three values are positive (greater than zero) before applying changes—if any value fails validation, reject the entire update and return 0.
  • main.c: Demonstrate your encapsulated Box by creating one, checking its volume, attempting dimension changes (both valid and invalid), and cleaning up properly.

You will receive five inputs: the initial width, initial height, initial depth, followed by three new dimensions to attempt setting (which may include invalid values).

In your main file, create a Box with the initial dimensions and print its volume. Then attempt to set the new dimensions—print whether the update succeeded and the resulting volume. Finally, free the box and confirm cleanup.

Print the output in this format:

Volume: {volume}
Set: {0 or 1}
Volume: {volume}
Destroyed

For example, with inputs 2, 3, 4, 5, 5, 5, the output would be:

Volume: 24
Set: 1
Volume: 125
Destroyed

With inputs 2, 3, 4, -1, 5, 5, the output would be:

Volume: 24
Set: 0
Volume: 24
Destroyed

Notice how the validation in set_dimensions protects your box from invalid states—external code cannot bypass your rules because the dimensions are completely hidden behind your interface.

Try it yourself

#include <stdio.h>
#include "box.h"

int main() {
    // Read initial dimensions
    int width, height, depth;
    scanf("%d", &width);
    scanf("%d", &height);
    scanf("%d", &depth);
    
    // Read new dimensions to attempt
    int new_w, new_h, new_d;
    scanf("%d", &new_w);
    scanf("%d", &new_h);
    scanf("%d", &new_d);
    
    // TODO: Create a Box with initial dimensions
    
    
    // TODO: Print initial volume in format: "Volume: {volume}"
    
    
    // TODO: Attempt to set new dimensions and store the result
    
    
    // TODO: Print whether set succeeded in format: "Set: {0 or 1}"
    
    
    // TODO: Print the resulting volume in format: "Volume: {volume}"
    
    
    // TODO: Destroy the box and print "Destroyed"
    
    
    return 0;
}

All lessons in Object Oriented Programming