Recap: Secret Box
Part of the Object Oriented Programming section of Coddy's C journey — lesson 21 of 61.
Challenge
EasyLet'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 opaqueBoxtype 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 pointerdestroy_box— frees the allocated memoryget_volume— takes aconst Box *and returns the volume (width × height × depth)set_dimensions— takes a Box pointer and three new dimension values, returnsint(1 for success, 0 if any dimension is negative or zero)
BOX_H.box.c: Define the actualstruct Boxwith three hidden members:int width,int height, andint depth. Implement all four functions. Your constructor should allocate and initialize the box. Theset_dimensionsfunction 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}
DestroyedFor example, with inputs 2, 3, 4, 5, 5, 5, the output would be:
Volume: 24
Set: 1
Volume: 125
DestroyedWith inputs 2, 3, 4, -1, 5, 5, the output would be:
Volume: 24
Set: 0
Volume: 24
DestroyedNotice 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
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy