Concept Explanation
This lesson is about structure. Instead of placing every detail in one file, you will look at a tiny split between declaration and implementation and think about why that matters as a program grows. The goal is not to memorize build-system rules, but to understand responsibilities: where interfaces belong, where behavior belongs, and how that separation helps you test, reuse, and change code with less confusion. By the end, you should be able to describe a simple C++ layout where each file has a clear job and the whole example remains easy to navigate.
Where to Put the Code
- Start with variables and inputs. Keep includes, main function, and data types explicit.
- Add processing logic in the middle section.
- Finish with output and quick validation.
Command Reference
- Describe the purpose of the header file in one sentence before you change any code.
- Modify the function name or behavior once, then update both declaration and definition consistently.
- Create a tiny main file mentally or on paper that would call add(2, 3).
- Record one reason separating interface from implementation becomes useful in larger projects.
Step-by-step Guide
- Read both parts of the example and identify which file exposes the interface.
- Explain what would break if the declaration and definition no longer matched.
- Make one small change, such as renaming the function or adjusting the return expression, and keep the files in sync.
- Check the layout again and confirm that each file still has one clear responsibility.
- End with a short checklist covering naming, consistency, and ease of reuse.
Practice Exercises
- Add a second function declaration and definition for subtract while keeping the same file structure.
- Write a simple main.cpp example that includes the header and prints the result of one function call.
- Refactor the naming so the utility file sounds more specific to its purpose.
Coding Challenges
- Compare this split-file approach with a single-file version and explain which is easier to maintain as features grow.
- Design a tiny utility module with two related functions and keep the interface clean and minimal.
Mini Practice Tasks
- Add one extra function prototype to the header.
- Write a one-line summary of what this code organization achieves.
- Check that the declaration and definition use the same name and parameter order.
Common Mistake
Skipping input validation or mixing logic/output in one unstructured block.
Real-life Mini Challenge
Build a small real-life example for this lesson topic using 3 clear steps: input, process, output.