Shape Container
Part of the Object Oriented Programming section of Coddy's C journey — lesson 48 of 61.
Challenge
EasyLet's bring the Shape Drawer project to its grand finale! You'll create a shape container that holds a mixed collection of Circles and Rectangles, then iterate through them to compute the total area — demonstrating the true power of polymorphism.
Building on everything you've created so far, your complete project will include:
shape.handshape.c: Your base Shape interface and theprocess_shapefunction.circle.handcircle.c: Your Circle implementation with its constructor and area calculation.rectangle.handrectangle.c: Your Rectangle implementation with its constructor and area calculation.main.c: This is where the magic happens. You'll create an array ofShape*pointers that can hold any shape type. Populate it with a mix of Circles and Rectangles, then loop through the array to calculate and display the total area of all shapes combined.
Your program will receive the following inputs:
- The number of shapes to create
- For each shape: a type indicator (
cfor circle,rfor rectangle) followed by its dimensions
For each shape in your container, print its draw output. After processing all shapes, print the total area with 2 decimal places in the format Total Area: X.XX.
Example with inputs 3, then c 5.0, r 4.0 3.0, c 2.0:
Drawing Circle with radius: 5.00
Drawing Rectangle with width: 4.00 and height: 3.00
Drawing Circle with radius: 2.00
Total Area: 103.11Example with inputs 2, then r 6.0 4.0, r 3.0 2.0:
Drawing Rectangle with width: 6.00 and height: 4.00
Drawing Rectangle with width: 3.00 and height: 2.00
Total Area: 30.00The key insight is that your array stores Shape* pointers — it doesn't care whether each element is actually a Circle or Rectangle. When you iterate and call draw and area through each pointer, the correct implementation executes automatically based on how each shape was constructed. This is polymorphism at work: one loop, one interface, multiple behaviors.
Remember that you'll need to store pointers to the embedded Shape members of your concrete shapes in the array. Use 3.14159 for π in circle area calculations.
Important — reading the shape type character: After reading the number of shapes with scanf("%d", &n), each subsequent call to read the type character must skip the leftover newline in the input buffer. Use a leading space in the format string to do this:
scanf(" %c", &type); // note the space before %cWithout the leading space, scanf will read the newline character left over from the previous input instead of the intended c or r, causing your shape type check to fail silently and produce no output.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
#include "shape.h"
#include "circle.h"
#include "rectangle.h"
int main() {
// TODO: Read the number of shapes
// TODO: Allocate arrays for circles, rectangles, and Shape* pointers
// TODO: Loop through each shape, read its type and dimensions,
// create the shape, and store a pointer to its base in the shapes array
// TODO: Loop through the shapes array, call draw on each shape,
// and accumulate the total area
// TODO: Print the total area in the format: Total Area: X.XX
// TODO: Free allocated memory
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 Hierarchy9Project: Shape Drawer
Project OverviewCircle Implementation