Rectangle Implementation
Part of the Object Oriented Programming section of Coddy's C journey — lesson 46 of 61.
Challenge
EasyLet's continue building the Shape Drawer project by implementing the Rectangle — another concrete shape that embeds the base Shape struct, just like we did with Circle.
Building on your existing project, you'll add rectangle functionality across these files:
shape.h: Keep your baseShapeinterface with theDrawFuncandAreaFuncfunction pointer types from the previous lessons.circle.handcircle.c: Keep your Circle implementation from the previous lesson.rectangle.h: Define yourRectanglestruct here. A rectangle embedsShapeas its first member and addswidthandheightfields (bothdouble). Declare a constructor functioncreate_rectanglethat takes width and height parameters and returns aRectangleby value.rectangle.c: Implement the rectangle-specific behavior:draw_rectangle— printsDrawing Rectangle with width: X.XX and height: X.XX(both values shown with 2 decimal places)area_rectangle— returns width × heightcreate_rectangle— initializes aRectangle, wires up the embeddedShape's function pointers to the rectangle functions, sets the dimensions, and returns the rectangle
main.c: Demonstrate both shapes working through the same interface. Read two inputs: width and height for a rectangle. Create a rectangle using your constructor, then call bothdrawandareathrough the embeddedShape's function pointers. Print the area with 2 decimal places.
Your program will receive two inputs: width and height values (as floating-point numbers).
Example output when the inputs are 4.0 and 3.0:
Drawing Rectangle with width: 4.00 and height: 3.00
Area: 12.00Example output when the inputs are 7.5 and 2.5:
Drawing Rectangle with width: 7.50 and height: 2.50
Area: 18.75Just like with Circle, the Rectangle embeds Shape as its first member, allowing you to access the function pointers through the embedded member. This consistent pattern is what enables polymorphism — both shapes can be treated uniformly through their Shape interface. Remember to use include guards in all header files.
Try it yourself
#include <stdio.h>
#include "shape.h"
#include "rectangle.h"
int main() {
double width, height;
scanf("%lf", &width);
scanf("%lf", &height);
// TODO: Create a rectangle using create_rectangle(width, height)
// TODO: Call draw through the embedded Shape's function pointer
// TODO: Call area through the embedded Shape's function pointer
// TODO: Print the area with 2 decimal places: "Area: %.2f\n"
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