Recap: Point Manager
Part of the Object Oriented Programming section of Coddy's C journey — lesson 11 of 61.
Challenge
EasyLet's build a complete Point module that manages a 2D coordinate. This recap challenge brings together everything you've learned about treating structs as objects—using pointers to modify state and const pointers for read-only operations.
You'll create three files:
point.h: Declare aPointstruct with two integer members:xandy. Also declare two functions that act as methods:point_move— takes a pointer to Point along with two integers (dxanddy), and shifts the point's position by adding these values toxandypoint_print— takes aconstpointer to Point and displays its coordinates (this function should not modify the point)
POINT_H.point.c: Implement both functions. Think carefully about which function needs a regular pointer (to modify the point) and which uses aconstpointer (to safely read without modification).main.c: Create a Point, move it around, and display its position at different stages.
You will receive four integer inputs: the initial x, the initial y, the dx value (amount to move horizontally), and the dy value (amount to move vertically).
In your main file, initialize a Point with the starting coordinates, print its initial position, move it by the given amounts, then print its new position.
The point_print function should output in this format:
Point: ({x}, {y})For example, with inputs 3, 5, 2, and -1, the output would be:
Point: (3, 5)
Point: (5, 4)Try it yourself
#include <stdio.h>
#include "point.h"
int main() {
// Read input values
int x, y, dx, dy;
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &dx);
scanf("%d", &dy);
// TODO: Create a Point with initial coordinates (x, y)
// TODO: Print the initial position using point_print
// TODO: Move the point by (dx, dy) using point_move
// TODO: Print the new position using point_print
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