Circle Implementation
Part of the Object Oriented Programming section of Coddy's C journey — lesson 45 of 61.
Challenge
EasyLet's continue building the Shape Drawer project by implementing the Circle — a concrete shape that embeds the base Shape struct and provides its own drawing and area logic.
Building on the foundation from the previous lesson, you'll extend your project with these files:
shape.h: Keep your baseShapeinterface from the previous lesson with theDrawFuncandAreaFuncfunction pointer types.circle.h: Define yourCirclestruct here. A circle embedsShapeas its first member (enabling the first member rule for upcasting) and adds aradiusfield of typedouble. Declare a constructor functioncreate_circlethat takes a radius and returns aCircleby value.circle.c: Implement the circle-specific behavior here:draw_circle— printsDrawing Circle with radius: X.XX(radius shown with 2 decimal places)area_circle— returns π × radius² (use3.14159for π)create_circle— initializes aCircle, wires up the embeddedShape's function pointers to point todraw_circleandarea_circle, sets the radius, and returns the circle
main.c: Bring everything together. Read a radius value as input, create a circle using your constructor, then call bothdrawandareathrough the embeddedShape's function pointers. Print the area with 2 decimal places.
Your program will receive one input: a radius value (as a floating-point number).
Example output when the input is 5.0:
Drawing Circle with radius: 5.00
Area: 78.54Example output when the input is 3.5:
Drawing Circle with radius: 3.50
Area: 38.48The key insight is that Circle contains a Shape as its first member, so you can access the function pointers through circle.base.draw and circle.base.area (or whatever you name the embedded member). When calling these functions, pass a pointer to the Shape member. Remember to use include guards in all header files.
Try it yourself
#include <stdio.h>
#include "shape.h"
#include "circle.h"
int main() {
double radius;
scanf("%lf", &radius);
// TODO: Create a Circle using create_circle
// TODO: Call draw through the embedded Shape's function pointer
// TODO: Call area through the embedded Shape's function pointer
// and print 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