Recap: Shape Hierarchy
Part of the Object Oriented Programming section of Coddy's C journey — lesson 32 of 61.
Challenge
EasyLet's build a complete shape hierarchy that demonstrates the power of inheritance via composition. You'll create a base Shape type that any specific shape can inherit from, then build a Rect that extends it.
You'll organize your code across three files:
shape.h: Define your struct hierarchy with include guards. Create aShapestruct with a singlecolorfield (an integer). Then define aRectstruct that embedsShapeas its first member and addswidthandheightfields (both integers). Declare two functions:set_colorthat takes aShape*and a new color value, andprint_rectthat takes aRect*.shape.c: Implement both functions. Yourset_colorfunction works with the base type—it modifies the color of any shape. Yourprint_rectfunction displays all the rectangle's information including its inherited color.main.c: Create aRectand initialize all its fields. Then demonstrate upcasting by callingset_colorwith your rectangle cast to aShape*. Finally, print the rectangle to verify the color was changed through the base type pointer.
You will receive four inputs: the rectangle's width, its height, an initial color, and a new color to set via the generic function.
In your main file, create the rectangle with the initial values, then use set_color to change its color to the new value (remember to cast your Rect* to Shape*), and finally call print_rect to display the result.
Your output should look like this:
Width: 10
Height: 5
Color: 7Where 10 is the width, 5 is the height, and 7 is the updated color (not the initial color). The key insight here is that set_color knows nothing about rectangles—it only works with Shape. Yet through the first member rule and upcasting, your rectangle's color gets modified through this generic function.
Try it yourself
#include <stdio.h>
#include "shape.h"
int main() {
int width, height, initial_color, new_color;
scanf("%d", &width);
scanf("%d", &height);
scanf("%d", &initial_color);
scanf("%d", &new_color);
// TODO: Create a Rect and initialize all its fields
// (width, height, and the embedded Shape's color with initial_color)
// TODO: Call set_color with the rectangle cast to Shape*
// to change the color to new_color
// TODO: Call print_rect to display the result
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